gpt4 book ai didi

c# - 如何将 ComboBox 绑定(bind)到 DataTemplate 中的所有枚举类型?

转载 作者:行者123 更新时间:2023-11-30 18:31:30 25 4
gpt4 key购买 nike

我想根据绑定(bind)类型显示一个框架 UI 元素。对于枚举类型,我想显示 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 上做到这一点?

更新:目前,我使用 2 个转换器解决了它。第一个返回 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;
}

对于 SelectedValue,第二个将枚举转换为字符串,反之亦然:

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>

最佳答案

请看这个:

  <Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="AlignmentValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="HorizontalAlignment" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>

顺便说一句,谷歌是你的 friend 。在此处发帖之前,请务必先询问谷歌。

我在发布答案之前也这样做了,我为您找到了这个链接

http://msdn.microsoft.com/en-us/library/bb613576.aspx

关于c# - 如何将 ComboBox 绑定(bind)到 DataTemplate 中的所有枚举类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com