Is it possible to template a template in WPF XAML?

Posted by imekon on Stack Overflow See other posts from Stack Overflow or by imekon
Published on 2010-05-15T09:28:35Z Indexed on 2010/05/15 9:34 UTC
Read the original article Hit count: 393

Filed under:
|
|

Is it possible to use templates within templates?

For instance, I have the following two templates:

<HierarchicalDataTemplate 
    x:Key="RecursiveTemplate"
    ItemsSource="{Binding Children}">
    <StackPanel Margin="1" Orientation="Horizontal">
        <Ellipse Fill="DarkGreen" Width="14" Height="14"/>
        <TextBlock 
            MouseDown="OnTreeMouseDown" 
            TargetUpdated="OnTargetUpdated"
            Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}" 
            Margin="5" 
            Text="{Binding Name}"/>
        <TextBox 
            PreviewKeyDown="OnTreeKeyDown" 
            Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}" 
            Margin="2" 
            Text="{Binding Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate 
    x:Key="ContainerTemplate"
    ItemsSource="{Binding Children}">
    <StackPanel Margin="1" Orientation="Horizontal">
        <Ellipse Fill="DarkBlue" Width="14" Height="14"/>
        <TextBlock 
            MouseDown="OnTreeMouseDown" 
            TargetUpdated="OnTargetUpdated"
            Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}" 
            Margin="5" 
            Text="{Binding Name}"/>
        <TextBox 
            PreviewKeyDown="OnTreeKeyDown" 
            Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}" 
            Margin="2" 
            Text="{Binding Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>

There's a section of identical XAML:

<TextBlock 
     MouseDown="OnTreeMouseDown" 
     TargetUpdated="OnTargetUpdated"
     Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}" 
     Margin="5" 
     Text="{Binding Name}"/>
<TextBox 
     PreviewKeyDown="OnTreeKeyDown" 
     Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}" 
     Margin="2" 
     Text="{Binding Name}"/>

Is it possible to move that to a resource and refer to it by name, rather than repeat it?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml