Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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_Xaml - Fatal编程技术网

C# 创建带有自定义类绑定的复选框组合框

C# 创建带有自定义类绑定的复选框组合框,c#,wpf,xaml,C#,Wpf,Xaml,非常简单,我有一个用于字符串和布尔的容器类: public class Filter { public Filter(string field, bool chec = false) { Field = field; Checked = chec; } public String Field { get; set; } public bool Check

非常简单,我有一个用于字符串和布尔的容器类:

public class Filter
        {
        public Filter(string field, bool chec = false)
        {
            Field = field;
            Checked = chec;
        }
        public String Field { get; set; }
        public bool Checked { get; set; }
    }
我在另一个类中有一个过滤器列表:

public class FilterBundle
{
    public List<Filter> Fields { get; set; }
...
公共类筛选器绑定
{
公共列表字段{get;set;}
...
现在,我创建一个FilterBundle(filterBundle1)并尝试将组合框绑定到其Fields属性:

<ComboBox Grid.Column="1"
    ItemsSource="{Binding filterBundle1.Fields}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Field}"
                    IsChecked="{Binding Checked}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
</ComboBox>


但是,下拉列表是空的。在这两个类的getter中是否需要执行某些操作,以允许访问字段并检查filterBundle1列表中的每个筛选器?

尝试使用
ObservableCollection
作为
项源
而不是
列表

public ObservableCollection<Filter> Fields { get; set; }
publicobservableCollection字段{get;set;}
您可以通过以下方式轻松地将列表转换为一个列表:

Fields = new ObservableCollection<Filter>(MyFieldList);
Fields=新的ObservableCollection(MyFieldList);

通常,WPF取决于集合和属性,并分别实现
INotifyCollectionChanged
INotifyPropertyChanged
以更新UI。

没问题。如果找到答案,请向上投票并选中。:)