Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-更改组合框中枚举的字符串值_C#_Wpf_Combobox_Enums - Fatal编程技术网

C# WPF-更改组合框中枚举的字符串值

C# WPF-更改组合框中枚举的字符串值,c#,wpf,combobox,enums,C#,Wpf,Combobox,Enums,我有一个枚举,我使用它的值作为组合框中的选项 枚举如下所示: public enum TimeSizeEnum { TENTHSECONDS, SECONDS, MINUTES, HOURS } 将值绑定到组合框的方式: <ComboBox Grid.Row="4" Grid.Column="1" ItemsSource="{Binding Path=TimeSizeItemsSource, Converter={StaticResource TimerS

我有一个枚举,我使用它的值作为组合框中的选项

枚举如下所示:

public enum TimeSizeEnum
{
    TENTHSECONDS,
    SECONDS,
    MINUTES,
    HOURS
}
将值绑定到组合框的方式:

<ComboBox Grid.Row="4" Grid.Column="1" ItemsSource="{Binding Path=TimeSizeItemsSource, Converter={StaticResource TimerSizeConverter}}" SelectedItem="{Binding Path=TimeSize, Mode=TwoWay}" SelectedIndex="0" Margin="5" MinWidth="100"></ComboBox>

public string[] TimeSizeItemsSource
{
    get
    {
        return Enum.GetNames(typeof(TimeSizeEnum));
    }
}

公共字符串[]TimeSizeItemsSource
{
得到
{
返回Enum.GetNames(typeof(timesizenum));
}
}
我想要的不是十分之一秒,而是十分之一秒,或者不是秒,而是秒


我怎样才能做到这一点?值转换器是最好的方法吗?但这意味着我需要硬编码我想要的字符串。

您可以将属性放在枚举成员上,如

public enum TimeSizeEnum
{
    [Description("Tenth of a second")]
    TENTHSECONDS,
    [Description("Seconds")]
    SECONDS,
}
然后您可以编写一个转换器,从传递的值中读取并返回这些属性,即在您可以编写的IValueConverter的Convert方法中

var enumtype = typeof(TimeSizeEnum);
var memInfo = enumtype.GetMember(value.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),
    false);
var description = ((DescriptionAttribute)attributes[0]).Description;

return description
我建议使用:

然后,您可以检查在
ValueConverter
中传递的
enum
值,从属性中读取描述,并显示:

public class TimeSizeEnumDescriptionValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var type = typeof(TimeSizeEnum);
        var name = Enum.GetName(type, value);
        FieldInfo fi = type.GetField(name);
        var descriptionAttrib = (DescriptionAttribute)
            Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));

        return descriptionAttrib.Description;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
要将值转换器应用于每个枚举值,需要更改组合框的
ItemTemplate
以包含值转换器:

<Window.Resources>
    <test:TimeSizeEnumDescriptionValueConverter x:Key="converter" />
</Window.Resources>

<!-- ... -->

<ComboBox ItemsSource="{Binding TimeSizeItemsSource}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentPresenter
                Content="{Binding Converter={StaticResource converter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>


答案的另一种选择是使用ValueConverter和资源字典。考虑到我为所有的Enums使用标记扩展,但是它们使用的描述和类别属性很像一个值转换器。你看到我把EnUM绑定到组合框的ItEsStand属性了吗?这意味着我在值转换器中得到了整个枚举值集(字符串[]),而不是枚举的单个值,除非它是用于非常小的应用程序,否则我强烈建议不要对与UI相关的任何内容使用
DescriptionAttribute
。它不仅不可本地化,而且您可能还需要不同的描述来适应不同的UI(短UI、长UI等)。您看到我将枚举绑定到Combobox的ItemsSource属性了吗?这意味着我在值转换器中得到了整个枚举值集(字符串[]),而不是枚举的单个值。我在测试此解决方案时遇到了相同的问题,但这很容易解决-您需要添加一个
ContentPresenter
,将值转换器包括在组合框的
ItemTemplate
中,以将其应用于每个
enum
值。更新了以上我的答案。除非是针对非常小的应用程序,否则我强烈建议不要对任何与UI相关的应用程序使用
DescriptionAttribute
。它不仅不可本地化,而且您可能还需要不同的描述来适应不同的UI(短UI、长UI等)。
<Window.Resources>
    <test:TimeSizeEnumDescriptionValueConverter x:Key="converter" />
</Window.Resources>

<!-- ... -->

<ComboBox ItemsSource="{Binding TimeSizeItemsSource}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentPresenter
                Content="{Binding Converter={StaticResource converter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>