Wpf 如何组合看似多余的XAML

Wpf 如何组合看似多余的XAML,wpf,datatemplate,Wpf,Datatemplate,我有8个不同的XAML数据模板,它们都非常相似。以下是其中两个: <DataTemplate x:Key="ConflictFieldStringCellContentTemplate"> <StackPanel> <TextBlock Text="{Binding ClientVersion.Value}" Foreground="{Binding Path=Mismatch, Converter={S

我有8个不同的XAML数据模板,它们都非常相似。以下是其中两个:

<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">
    <StackPanel>
        <TextBlock Text="{Binding ClientVersion.Value}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}" />
        <Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
        <TextBlock Text="{Binding ServerVersion.Value}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
    </StackPanel>
</DataTemplate>

<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
    <StackPanel>
        <TextBlock Text="{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
        <Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
        <TextBlock Text="{Binding ServerVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
    </StackPanel>
</DataTemplate>

如您所见,唯一的区别是它们使用不同的转换器绑定TextBlock的Text属性。我有没有办法找出这两个数据模板的共同点?我还有6个,更新它们变得非常单调乏味,因为除了用于绑定文本属性的转换器之外,所有内容都是相同的

有没有一种方法可以将其分解成一个可以参数化的模板?类似这样的东西会很酷(伪代码):


{Binding ClientVersion.Value}
{Binding ClientVersion.Value,Converter={StaticResource stringArrayToCommaDelimitedStringConverter}

您可以尝试的一种方法是创建新的

此用户控件应包含StackPanel,此StackPanel应包含TextBox、Label和TextBox

您可以将TextConverter实现为依赖属性

最后一组数据模板如下所示:

    <DataTemplate x:Key="ConflictFieldStringCellContentTemplate">    
       <local:VersionDisplayControl 
                  ClientVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}" />
    </DataTemplate>

   <DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
       <local:VersionDisplayControl 
          ClientVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}"
          ServerVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}" />
    </DataTemplate>

这是假设用户控件能够从一些全局可用的源访问源版本信息。
如果没有,VersionDisplayControl将不得不公开另一个公共属性,可能称为VersionSource。

如果只有一个值,并且您希望仅使用模板执行此操作,则可以执行以下操作:

<DataTemplate x:Key="VersionDisplayTemplate">
    <StackPanel>
        <TextBlock Text="{TemplateBinding Tag}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}" />
        <Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
        <TextBlock Text="{TemplateBinding Content}"
                   Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
    </StackPanel>
</DataTemplate>

现在,您可以将其用作:

<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">    
   <ContentPresenter 
       Tag="ABC"
       Content="{Binding ClientVersion.Value}" 
       ContentTemplate="{StaticResource VersionDisplayTemplate}" 
       />
</DataTemplate>

<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
   <ContentPresenter 
       Tag="XYZ"
       Content="{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}" 
       ContentTemplate="{StaticResource VersionDisplayTemplate}"
       />
</DataTemplate>

<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">    
   <ContentPresenter 
       Tag="ABC"
       Content="{Binding ClientVersion.Value}" 
       ContentTemplate="{StaticResource VersionDisplayTemplate}" 
       />
</DataTemplate>

<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
   <ContentPresenter 
       Tag="XYZ"
       Content="{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}" 
       ContentTemplate="{StaticResource VersionDisplayTemplate}"
       />
</DataTemplate>