MyNewName MyNewName - 3 months ago 7
C# Question

DropDown bind to property names; based on first set second DropDown binding

I have one

ObservableCollection<M> fooBar {get;set;}
. The class
M.cs
looks like this:

public class M{
private int _ID;
public int ID {
get {return this._ID;}
set {this._ID = value;}
}

private string _number;
public int Number {
get {return this._number;}
set {this._number = value;}
}

private string _power;
public int Power {
get {return this._power;}
set {this._power = value;}
}

/*
...
*/
}


Now I want to bind the names of these 3 propertys to a
ComboBox
. I don't want to do it like this:

<ComboBox>
<ComboBoxItem>ID</ComboBoxItem>
<ComboBoxItem>Number</ComboBoxItem>
<ComboBoxItem>Power</ComboBoxItem>
</ComboBox>


Is there a more comfortable way?

Based on the choose of the first
ComboBox
I want to fill the second
ComboBox
. As example I choose in the first
ComboBox
the property
Number
then the second
ComboBox
should look like this

<ComboBox
SelectedValue="{Binding ???}"
ItemsSource="{Binding fooBar}"
SelectedValuePath="Number"
DisplayMemberPath="Number"
/>


Maybe someone of you can help me, because I have no idea how to connect both comboboxes.

Answer

This is how I would do it:

  • Make a property on the view model which exposes the properties on the model (class M) which can be selected. This way you explicitly control which properties can be selected.

  • Make a property to hold the selected value of each combo box.

  • DisplayMemberPath/SelectedValuePath in ComboBox2 binds to the SelectedValue of ComboBox1.

ViewModel:

        // ComboBox 1
        public Dictionary<string, string> SelectableProperties = new Dictionary<string, string>()
        {
            { nameof (M.ID), "ID" }
            { nameof (M.Number), "Nummer" }
            { nameof (M.Power), "Potenz" }
        }

        // Selection in combobox 1 (not strictly necessary as it can be handled in view, but you may need to know what SelectedValue represents)
        private string _selectedValueMember = String.Empty;
        public string SelectedValueMember
        {
            get { return _selectedValueMember; }
            set { _selectedValueMember = value; }
        }

        // Selection in combobox 2 (object just in case there could be other values than int) 
        private object _selectedValue = null;
        public object SelectedValue
        {
            get { return _selectedValue; }
            set { _selectedValue = value; }
        }

        public ObservableCollection<M> FooBar{ get; set; }

View:

<ComboBox x:Name="ComboBox1"
                  Width="100"
                  Margin="5"
                  SelectedValuePath="Key"
                  DisplayMemberPath="Value"
                  SelectedValue="{Binding SelectedValueMember}"
                  ItemsSource="{Binding SelectableProperties}">

        </ComboBox>
        <ComboBox Width="100"
                  Margin="5"
                  DisplayMemberPath="{Binding ElementName=ComboBox1, Path=SelectedValue}"
                  SelectedValuePath="{Binding ElementName=ComboBox1, Path=SelectedValue}"
                  SelectedValue="{Binding SelectedValue}"
                  ItemsSource="{Binding FooBar}">

        </ComboBox>
Comments