我有以下代码:
public class FieldViewerSelector : IMultiValueConverter
{
public DataTemplate PercentTemplate { get; set; }
public DataTemplate FileSizeTemplate { get; set; }
#region IValueConverter Members
//....
#endregion
}
<commonConverters:FieldValueConverter x:Key="FieldValueConverter" />
<commonConverters:PercentValueConverter x:Key="PercentValueConverter" />
<commonConverters:FileSizeValueConverter x:Key="FileSizeValueConverter" />
<commonConverters:FieldViewerSelector x:Key="FieldViewerSelector">
<commonConverters:FieldViewerSelector.FileSizeTemplate>
<DataTemplate>
<!--DataTemplate almost the same-->
</DataTemplate>
</commonConverters:FieldViewerSelector.FileSizeTemplate>
<commonConverters:PercentValueConverter>
<DataTemplate>
<!--DataTemplate almost the same-->
</DataTemplate>
</commonConverters:PercentValueConverter>
</commonConverters:FieldViewerSelector>
<DataTemplate x:Key="template">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource LeftColumn}" Text="{Binding Path=DisplayName, Mode=OneWay}"/>
<Border Background="{StaticResource LightGray}" Grid.Column="1">
<TextBox Style="{StaticResource ReadOnlyTextBox}">
<TextBox.Text>
<Binding
<!--ONLY THE FOLLOWING LINE IS DIFFERENT depends on property-->
Converter="{StaticResource PercentValueConverter}"
<!--Converter="{StaticResource FieldValueConverter}"-->
Mode="OneWay"
Path="DataContext.Item"
RelativeSource="{RelativeSource AncestorType=Window}"/>
</TextBox.Text>
</TextBox>
</Border>
</Grid>
</DataTemplate>
所以我不知道如何通过绑定或任何其他方式使用正确的转换器指定特定的DataTemplate。 我必须这样做才能不重复自己=)