Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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/4/wpf/14.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# 将布尔值绑定到组合框_C#_Wpf_Data Binding_Combobox - Fatal编程技术网

C# 将布尔值绑定到组合框

C# 将布尔值绑定到组合框,c#,wpf,data-binding,combobox,C#,Wpf,Data Binding,Combobox,我有一个组合框,我想编辑一个布尔值 <dxe:ComboBoxEdit ItemsSource="{Binding EnumItemsSource}" DisplayMember="Name" ValueMember="Id" IsTextEditable="False"

我有一个组合框,我想编辑一个布尔值

        <dxe:ComboBoxEdit ItemsSource="{Binding EnumItemsSource}" 
                          DisplayMember="Name"
                          ValueMember="Id"
                          IsTextEditable="False"
                          EditValue="{Binding TargetValue, UpdateSourceTrigger=PropertyChanged}"/>

我的ViewModel:

    /// <summary>
    /// Contains the ItemsSource for Enums
    /// </summary>
    public List<EnumItemObject> EnumItemsSource
    {
        get { return _enumItemsSource; }
        set
        {
            _enumItemsSource = value;
            OnPropertyChanged();
        }
    }

public class EnumItemObject
{
   public int Id {get;set;}
   public string Name {get;set;}
}
//
///包含枚举的ItemsSource
/// 
公共列表EnumItemsSource
{
获取{return}enumItemsSource;}
设置
{
_enumItemsSource=值;
OnPropertyChanged();
}
}
公共类EnumItemObject
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
我为Combobox ItemsSource准备数据:

    /// <summary>
    /// Sets the value to the properties for the BitTemplate view. (similar with EnumTemplate)
    /// </summary>
    /// <param name="propertyInfo">a boolean property</param>
    private void PrepareDataForBitTemplate(PropertyInfo propertyInfo)
    {
        TargetValue = (int)propertyInfo.GetValue(_firstSelectedItem);
        EnumItemsSource = new List<EnumItemObject>();
        EnumItemsSource.Add(new EnumItemObject() { Id = 0, Name = "Nein" });
        EnumItemsSource.Add(new EnumItemObject() { Id = 1, Name = "Ja" });
    }
//
///将该值设置为BitTemplate视图的属性。(与EnumTemplate类似)
/// 
///布尔属性
private void PreparedataForbTemplate(PropertyInfo PropertyInfo)
{
TargetValue=(int)propertyInfo.GetValue(_firstSelectedItem);
EnumItemsSource=新列表();
添加(新的EnumItemObject(){Id=0,Name=“Nein”});
添加(新的EnumItemObject(){Id=1,Name=“Ja”});
}
这种方法正确吗?有更简单的解决方案吗


谢谢

在WPF中更自然的方法是使用。简言之,它是一个对象,用于操纵属性值如何绑定到UI对象

在您的情况下,这意味着(例如,肯定有更多的方法)创建一个转换器,将布尔值转换为
0
1
,然后您可以使用预定义的文本Nein/Ja值(在正确的索引上)将该属性绑定到
组合框的
SelectedIndex
用那个转换器。

一个不相关的事情:如果你用另一种语言中的GUI文本开发你的应用程序,而不是英语,我会考虑把它移动到一个带有英文名字的资源文件中,以使代码更适合那些不懂德语的人。可能您不需要它,我只是觉得我应该提一下:)

如果您的代码正常工作,这可能是一个更好的问题。嗨,Paco,我不知道代码审查。谢谢你的提示。我把它贴在那里。谢谢