Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding to dictionary with classes

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

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:

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:

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:

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

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:

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.

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

like image 595
Patcher56 Avatar asked Oct 31 '22 22:10

Patcher56


1 Answers

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

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.

like image 107
Patcher56 Avatar answered Nov 15 '22 04:11

Patcher56