WPF使用类绑定到字典

时间:2022-09-04 17:04:11

I am new to WPF programming and not a professional in C# coding.

我是WPF编程的新手,而不是C#编码的专业人士。

So I have the following problem: I have a lot of different comboboxes and textboxes. I want to bind them to a variable in the background, because I want to fill the comboboxes with Data from a Database.

所以我有以下问题:我有很多不同的组合框和文本框。我想将它们绑定到后台的变量,因为我想用数据库中的数据填充组合框。

So I created the following in the file
liste.xaml.cs:

所以我在文件liste.xaml.cs中创建了以下内容:

private Dictionary<string, Filter> _ctrlFilter;

public Dictionary<string, Filter> ctrlFilter {
    get { return _ctrlFilter; }
    set { _ctrlFilter = value; }
}

When I load liste.xaml it initializes the ctrlFilter Dictionary with the following code:

当我加载liste.xaml时,它使用以下代码初始化ctrlFilter Dictionary:

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)) },
};

So the filter class (which is also in liste.xaml.cs) looks like this:

所以过滤器类(也在liste.xaml.cs中)看起来像这样:

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>();
    }
}

Now i dynamically load other xaml-files into the liste.xaml file. For example the Fahrzeug_Allgemein.xaml

现在我将其他xaml文件动态加载到liste.xaml文件中。例如Fahrzeug_Allgemein.xaml

There I have the comboboxes, which looks like this:

我有组合框,看起来像这样:

            <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>

You see that I tried to get the Items property from the Filter class, but it doesn't work. In the Visual Studio 2015 output it says:

您看到我尝试从Filter类中获取Items属性,但它不起作用。在Visual Studio 2015输出中,它说:

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')

I already read something about INotifyPropertyChanged but I do not know how to use it in my case.

我已经阅读了有关INotifyPropertyChanged的内容,但我不知道如何在我的情况下使用它。

It would be cool, if you could help me. Thank you.

如果你能帮助我,那会很酷。谢谢。

2 个解决方案

#1


1  

I haven't thought that the answer is so simple. I just had to add { get; set; } to the Items property.

我没想到答案很简单。我只需要添加{get;组;到Items属性。

So the code looks like this now:

所以代码现在看起来像这样:

public class Filter
{
    public string FilterName;
    public Type ControlType;
    public Type FromToType;
    public bool Load;
    public ObservableCollection<ComboBoxItem> Items { get; set; }
    ...

I think it means, that the Items property is able to bind only with this addition.

我认为这意味着,Items属性只能与此添加绑定。

#2


1  

I have too low repuatation to just comment this but you should read up on properties in c# and data binding if you have yet to do so. I read these links plus the extra links they contain and it helped increase my understanding significantly.

我的注释太低,只能对此进行评论,但是你应该阅读c#和数据绑定中的属性,如果你还没有这样做的话。我阅读了这些链接以及它们包含的额外链接,这有助于显着增加我的理解。

#1


1  

I haven't thought that the answer is so simple. I just had to add { get; set; } to the Items property.

我没想到答案很简单。我只需要添加{get;组;到Items属性。

So the code looks like this now:

所以代码现在看起来像这样:

public class Filter
{
    public string FilterName;
    public Type ControlType;
    public Type FromToType;
    public bool Load;
    public ObservableCollection<ComboBoxItem> Items { get; set; }
    ...

I think it means, that the Items property is able to bind only with this addition.

我认为这意味着,Items属性只能与此添加绑定。

#2


1  

I have too low repuatation to just comment this but you should read up on properties in c# and data binding if you have yet to do so. I read these links plus the extra links they contain and it helped increase my understanding significantly.

我的注释太低,只能对此进行评论,但是你应该阅读c#和数据绑定中的属性,如果你还没有这样做的话。我阅读了这些链接以及它们包含的额外链接,这有助于显着增加我的理解。