2010-05-15 88 views
2

是否可以在模板中使用模板?是否可以在WPF XAML中模板模板?

舉例來說,我有以下兩個模板:

<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> 

有相同的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}"/> 

是否有可能以移動到資源,並通過名稱引用它,而不是重複它?

回答

2

的答案似乎是「幾乎」

<Style x:Key="textBlockStyle" TargetType="TextBlock"> 
    <Setter Property="Visibility" Value="{Binding Editing, Converter={StaticResource visibilityInverter}}"/> 
    <Setter Property="Margin" Value="4"/> 
    <Setter Property="Text" Value="{Binding Name}"/> 
    <EventSetter Event="MouseDown" Handler="OnTreeMouseDown"/> 
</Style> 

<Style x:Key="textBoxStyle" TargetType="TextBox"> 
    <Setter Property="Visibility" Value="{Binding Editing, Converter={StaticResource visibilityConverter}}"/> 
    <Setter Property="Margin" Value="1"/> 
    <Setter Property="Text" Value="{Binding Name, NotifyOnSourceUpdated=True}"/> 
    <EventSetter Event="PreviewKeyDown" Handler="OnTreeKeyDown"/> 
</Style> 

然後模板減少

<HierarchicalDataTemplate 
    x:Key="RecursiveTemplate" 
    ItemsSource="{Binding Children}"> 
    <StackPanel Margin="1" Orientation="Horizontal"> 
     <Ellipse Fill="DarkGreen" Width="14" Height="14"/> 
     <TextBlock 
      Style="{StaticResource textBlockStyle}"/> 
     <TextBox 
      SourceUpdated="OnSourceUpdated" 
      Style="{StaticResource textBoxStyle}"/> 
    </StackPanel> 
</HierarchicalDataTemplate> 

但是,請注意,你不能把SourceUpdated的風格,因爲它不屬於EventSetter