Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 为ItemsSource使用IValueConverter代码隐藏_C#_Wpf - Fatal编程技术网

C# 为ItemsSource使用IValueConverter代码隐藏

C# 为ItemsSource使用IValueConverter代码隐藏,c#,wpf,C#,Wpf,我能够应用IValueConverter,它转换我的枚举并在xaml端显示Display name属性。我想知道如何在代码隐藏中执行相同的操作 EnumToDisplayAttribConverter.cs public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!value.GetType().IsEnum

我能够应用IValueConverter,它转换我的枚举并在xaml端显示Display name属性。我想知道如何在代码隐藏中执行相同的操作

EnumToDisplayAttribConverter.cs

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (!value.GetType().IsEnum)
    {
        throw new ArgumentException("Value must be an Enumeration type");
    }

    var fieldInfo = value.GetType().GetField(value.ToString());
    var array = fieldInfo.GetCustomAttributes(false);

    foreach (var attrib in array)
    {
        if (attrib is DisplayAttribute)
        {
            DisplayAttribute displayAttrib = attrib as DisplayAttribute;

            //if there is no resource assume we don't care about lization
            if (displayAttrib.ResourceType == null)
                return displayAttrib.Name;

            // per http://stackoverflow.com/questions/5015830/get-the-value-of-displayname-attribute
            ResourceManager resourceManager = new ResourceManager(displayAttrib.ResourceType.FullName, displayAttrib.ResourceType.Assembly);
            var entry =
                    resourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true)
                      .OfType<DictionaryEntry>()
                      .FirstOrDefault(p => p.Key.ToString() == displayAttrib.Name);

            var key = entry.Value.ToString();
            return key;
        }
    }

        //if we get here than there was no attrib, just pretty up the output by spacing on case
        // per http://stackoverflow.com/questions/155303/net-how-can-you-split-a-caps-delimited-string-into-an-array
        string name = Enum.GetName(value.GetType(), value);
        return Regex.Replace(name, "([a-z](?=[A-Z0-9])|[A-Z](?=[A-Z][a-z]))", "$1 ");
}\\
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性) { 如果(!value.GetType().IsEnum) { 抛出新ArgumentException(“值必须是枚举类型”); } var fieldInfo=value.GetType().GetField(value.ToString()); var数组=fieldInfo.GetCustomAttributes(false); foreach(数组中的var attrib) { if(attrib是DisplayAttribute) { DisplayAttribute displayAttrib=attrib作为DisplayAttribute; //如果没有资源,假设我们不关心资源化 if(displayAttrib.ResourceType==null) 返回displayAttrib.Name; //http://stackoverflow.com/questions/5015830/get-the-value-of-displayname-attribute ResourceManager ResourceManager=new ResourceManager(displayAttrib.ResourceType.FullName,displayAttrib.ResourceType.Assembly); 风险值分录= resourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture,true,true) 第()类 .FirstOrDefault(p=>p.Key.ToString()==displayAttrib.Name); var key=entry.Value.ToString(); 返回键; } } //如果我们到了这里,就没有attrib,只需通过大小写上的间距来调整输出 //http://stackoverflow.com/questions/155303/net-how-can-you-split-a-caps-delimited-string-into-an-array 字符串名称=Enum.GetName(value.GetType(),value); 返回正则表达式替换(名称“([a-z](?=[a-Z0-9])|[a-z](?=[a-z][a-z])”,“$1”); }\\ 我试过的-代码隐藏

var converter = new EnumToDisplayAttribConverter();
var converted =
        (IEnumerable<ReportArguements.NoteOptions>)
            converter.Convert(
                Enum.GetValues(typeof(ReportArguements.NoteOptions))
                    .Cast<ReportArguements.NoteOptions>(), typeof(IEnumerable<ReportArguements.NoteOptions>), null, null);

var notesOption = new ComboBox
{
    ItemsSource = converted,
    SelectedIndex = 0,
};
var converter=新的EnumToDisplayAttribConverter();
风险值转换=
(IEnumerable)
转换,转换(
Enum.GetValues(typeof(ReportArguements.NoteOptions))
.Cast(),typeof(IEnumerable),null,null);
var notesOption=新组合框
{
ItemsSource=已转换,
SelectedIndex=0,
};
请问我是否可以获得有关如何正确绑定到枚举以及如何使用代码隐藏将集合转换为使用“显示名称”属性的帮助

工作溶液

var datatemplate = new DataTemplate();
Binding binding = new Binding();
binding.Converter = new EnumToDisplayAttribConverter();

FrameworkElementFactory textElement = new FrameworkElementFactory(typeof(TextBlock));
textElement.SetBinding(TextBlock.TextProperty, binding);
datatemplate.VisualTree = textElement;

var notesOption = new ComboBox
{
    SelectedIndex = 0,
};

notesOption.ItemTemplate = datatemplate;
notesOption.ItemsSource = Enum.GetValues(typeof(ReportArguements.NoteOptions)).Cast<ReportArguements.NoteOptions>();
return notesOption;
var datatemplate=newdatatemplate();
绑定=新绑定();
binding.Converter=新的EnumToDisplayAttribConverter();
FrameworkElementFactory textElement=新的FrameworkElementFactory(typeof(TextBlock));
textElement.SetBinding(TextBlock.TextProperty,绑定);
datatemplate.VisualTree=textElement;
var notesOption=新组合框
{
SelectedIndex=0,
};
notesOption.ItemTemplate=数据模板;
notesOption.ItemsSource=Enum.GetValues(typeof(ReportArguements.NoteOptions)).Cast();
回执;

为什么不在组合框上使用自定义的
项目模板
,并在那里执行转换?这允许您选择实际的枚举值,而不必从它们的显示名称转换回来,并且不需要在代码中进行任何转换。@PieterWitvoet感谢您的建议,这绝对是一个合理的选项,因为我通常会将转换器放在ItemTemplate中。我将阅读如何在代码隐藏中设置ItemTemplate。那么在代码隐藏中这样做有什么特别的原因吗?嗨@PieterWitvoet我已经在XAML中完成了这项工作,我想尝试一下代码隐藏。请问ItemTemplate的错误在哪里?我刚刚尝试过,但转换器从未启动。我现在没有时间检查,但可能设置
notesOption.Style
(或者只是
notesOption.ItemTemplate
-不需要该样式)会显式工作?为什么不在
组合框上使用自定义
ItemTemplate
,然后在那里进行转换?这允许您选择实际的枚举值,而不必从它们的显示名称转换回来,并且不需要在代码中进行任何转换。@PieterWitvoet感谢您的建议,这绝对是一个合理的选项,因为我通常会将转换器放在ItemTemplate中。我将阅读如何在代码隐藏中设置ItemTemplate。那么在代码隐藏中这样做有什么特别的原因吗?嗨@PieterWitvoet我已经在XAML中完成了这项工作,我想尝试一下代码隐藏。请问ItemTemplate的错误在哪里?我刚刚尝试过,但转换器从未启动。我现在没有时间检查,但是可能显式设置
notesOption.Style
(或者只是
notesOption.ItemTemplate
-不需要样式)会起作用吗?