我希望根据绑定类型显示框架UI元素。
对于枚举类型,我想显示ComboBox。
我已经在XAML中有了这部分内容:
第二种方法将枚举转换为字符串并实现选定值的相互转换:
在XAML中,ComboBox将被定义如下所示:
我已经在XAML中有了这部分内容:
<StackPanel Grid.Column="2">
<StackPanel.Resources>
<ObjectDataProvider x:Key="EnumValues" ObjectType="{x:Type sys:Enum}" MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName= <-- How to indicate for all enum types? --> >
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<DataTemplate DataType="{x:Type sys:String}">
<TextBox Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentPresenter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0" />
</DataTemplate>
<DataTemplate DataType="{x:Type sys:Double}">
<TextBox Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentPresenter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0"/>
</DataTemplate>
<DataTemplate DataType="{x:Type sys:Enum}">
<ComboBox ItemsSource="{Binding Source={StaticResource EnumValues}}" SelectedValue="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentPresenter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0"/>
</DataTemplate>
</StackPanel.Resources>
<ContentPresenter Content="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</StackPanel>
请问有办法在XAML上实现吗?
更新: 目前,我使用了两个转换器来解决这个问题。 第一个转换器从给定的枚举中返回字符串集合作为ItemsSource:
/// <summary>
/// Converts the given value into a collection of strings
/// </summary>
/// <returns>collection of strings</returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
IEnumerable<Enum> enumsCollection = value.GetType().GetEnumValues().Cast<Enum>();
return enumsCollection.Select(enumValue => enumValue.ToString());
}
return null;
}
第二种方法将枚举转换为字符串并实现选定值的相互转换:
public class EnumToStringConverter : IValueConverter
{
#region Properties
/// <summary>
/// Last target type to be converted
/// </summary>
protected Type LastTargetType { get; set; }
#endregion
#region IValueConverter Members
/// <summary>
/// Converts a given value into a string representation
/// </summary>
/// <returns>string</returns>
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
LastTargetType = value.GetType();
return value.ToString();
}
return null;
}
/// <summary>
/// Converts back a given value into an enum representation
/// </summary>
/// <returns>enum</returns>
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Enum.Parse(LastTargetType, value.ToString());
}
#endregion
}
在XAML中,ComboBox将被定义如下所示:
<DataTemplate DataType="{x:Type sys:Enum}">
<ComboBox Name="EnumComboBox" ItemsSource="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentPresenter}, Converter={StaticResource EnumValuesConv}, Mode=OneTime}" SelectedValue="{Binding Content, Converter={StaticResource EnumToStringConv}, RelativeSource={RelativeSource AncestorType=ContentPresenter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Focusable="False" Width="100" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>