Wednesday, September 26, 2012

Include the templated Control into template?

Include the templated Control into template?

I want to template a Control and include the Control itself into the template, not only the control's content.

For example, I have a button:

<Grid x:Name="LayoutRoot" Background="White">          <Button Name="hello" Content="123" Width="100" Height="100" >         </Button>  </Grid> 

I want to make a broader to surround this button, so it will look like this:

<Grid x:Name="LayoutRoot" Background="White" Width="120" Height="120">          <Rectangle Fill="AliceBlue"/>         <Button Name="hello"  Content="123" Width="100" Height="100" >         </Button>  </Grid> 

But I want to make this more genric, not only for button, but for some other control. i.e Image, TreeViewItem etc.

So I created a template:

<UserControl.Resources>      <Style TargetType="Button">         <Setter Property="Template">             <Setter.Value>                 <ControlTemplate TargetType="Button">                     <Grid Width="120" Height="120">                         <Rectangle Fill="AliceBlue"/>                                                     <ContentPresenter  Content="{TemplateBinding Property=ContentControl.Content}" />                     </Grid>                 </ControlTemplate>             </Setter.Value>         </Setter>     </Style> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White">          <Button Name="hello"  Content="123" Width="100" Height="100" >         </Button> </Grid> 

Now the ContentPresenter only display the content of the Button, but not include the button itself, how can I include the templated control itself too?

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog