C# WPF绑定到具有类的字典
我是WPF编程新手,不是C#编码方面的专业人士 因此,我有以下问题: 我有很多不同的组合框和文本框。 我想将它们绑定到后台的变量,因为我想用数据库中的数据填充组合框 因此,我在文件中创建了以下内容C# WPF绑定到具有类的字典,c#,wpf,xaml,dictionary,data-binding,C#,Wpf,Xaml,Dictionary,Data Binding,我是WPF编程新手,不是C#编码方面的专业人士 因此,我有以下问题: 我有很多不同的组合框和文本框。 我想将它们绑定到后台的变量,因为我想用数据库中的数据填充组合框 因此,我在文件中创建了以下内容 liste.xaml.cs: 我已经读过一些关于INotifyPropertyChanged的内容,但我不知道如何在我的案例中使用它 如果你能帮助我,那就太好了。 谢谢。我没想到答案这么简单。我只需要将{get;set;}添加到Items属性中 现在代码看起来像这样: public class Fil
liste.xaml.cs: 我已经读过一些关于INotifyPropertyChanged的内容,但我不知道如何在我的案例中使用它 如果你能帮助我,那就太好了。
谢谢。我没想到答案这么简单。我只需要将
{get;set;}
添加到Items属性中
现在代码看起来像这样:
public class Filter
{
public string FilterName;
public Type ControlType;
public Type FromToType;
public bool Load;
public ObservableCollection<ComboBoxItem> Items { get; set; }
...
公共类过滤器
{
公共字符串过滤器名称;
公共类型控制类型;
公共类型FromToType;
公共负荷;
公共ObservableCollection项{get;set;}
...
我认为这意味着,Items属性只能通过此添加绑定。我的重新利用率太低,无法仅对此进行评论,但您应该这样做,如果您还没有这样做的话。我阅读了这些链接以及它们包含的额外链接,这有助于显著提高我的理解。您正在尝试绑定到词典……可能是这样的s将帮助您:准确地说,在添加get/set之前,它只是一个字段,而不是属性。
ctrlFilter = new Dictionary<string, Filter>
{
//ComboBox
{"hersteller", new Filter("Hersteller", typeof(ComboBox)) },
{"fahrzeugart", new Filter("Fahrzeugart", typeof(ComboBox), false) },
//TextBox
{"baujahr", new Filter("Baujahr", typeof(TextBox), true, typeof(short)) },
{"anschaffungswert", new Filter("Anschaffungswert", typeof(TextBox), true, typeof(decimal)) },
{"finanz_restwert", new Filter("Restwert", typeof(TextBox), true, typeof(decimal)) },
//Others
{"zugelassen", new Filter("Zugelassen", typeof(DatePicker), true, typeof(DateTime)) },
{"vstabzug", new Filter("Vorsteuerabzug", typeof(CheckBox), true, typeof(bool)) },
};
public class Filter
{
public string FilterName;
public Type ControlType;
public Type FromToType;
public bool Load;
public ObservableCollection<object> Items;
public object SelectedFilter;
public object From;
public object To;
public Filter( string name, Type type, bool load = true, Type ftType = null)
{
Reset();
FilterName = name;
ControlType = type;
Load = load;
FromToType = ftType;
}
public void Reset()
{
From = null;
To = null;
SelectedFilter = null;
Items = new ObservableCollection<object>();
}
}
<StackPanel>
<Label Content="Hersteller:" Target="{Binding ElementName=cmb_hersteller, Mode=OneWay}"/>
<ComboBox x:Name="cmb_hersteller" Fahrzeuge:FilterExtension.FilterName="hersteller" Width="120" ItemsSource="{Binding ctrlFilter[hersteller].Items}" SelectedIndex="0" SelectionChanged="cmb_hersteller_SelectionChanged"/>
</StackPanel>
System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''Filter' (HashCode=44888241)'. BindingExpression:Path=ctrlFilter[hersteller].Items; DataItem='liste' (Name=''); target element is 'ComboBox' (Name='cmb_hersteller'); target property is 'ItemsSource' (type 'IEnumerable')
public class Filter
{
public string FilterName;
public Type ControlType;
public Type FromToType;
public bool Load;
public ObservableCollection<ComboBoxItem> Items { get; set; }
...