Search code examples
c#.netxamlcomboboxselecteditem

How to select property from SelectedItem on ComboBox?


First of all, I've attempted a great deal of searching, but if my Google-fu appears to be lacking, feel free to instantly link me to another article/question that will answer.

I seem to be misunderstanding the usages of SelectedItem/SelectedValue even though I've read multiple articles and questions about them. Possibly I'm attempting to shoehorn the ComboBox bindings into something it was not meant to be.

public class MappedValue
{
    public string Representation
    {
        get;
        set;
    }
    public object Value
    {
        get;
        set;
    }
}

public class ParameterValue
{
    public object Value
    {
        get;
        set;
    }
}   

<ComboBox ItemsSource="{Binding Path=MappedValues}"
    DisplayMemberPath="Representation"
    SelectedItem="{Binding Path=ParameterValue.Value}"
    SelectedValue="{Binding Path=ParameterValue}"
    SelectedValuePath="Value"/>

'MappedValues' is just a collection.

So what I'm trying to do is pull the 'Value' property from the selected MappedValue and store that in 'ParameterValue.Value'.

So, as it is, it takes the entire selected MappedValue object (as opposed to the 'MappedValue.Value' property) and sets that as the 'ParameterValue.Value'.

Am I not understanding the function of these combobox properties, or is there no easy way to do this using XAML and ComboBox?


Solution

  • In my example I have used an ObservableCollection for storing a list of the class Person. If you implement the property changed interface you can then just set the selected item to a properties in the code behind. This way you will have full access to the properties of the objects in your list. I hope this helps

    Xaml

    <ComboBox x:Name="comboBox" 
                  HorizontalAlignment="Left" 
                  Margin="130,83,0,0" 
                  VerticalAlignment="Top" 
                  Width="120" 
                  ItemsSource="{Binding collection}"
                  DisplayMemberPath="firstName"
                  SelectedItem="{Binding SelectedPerson}"
                  SelectionChanged="comboBox_SelectionChanged"/>
        <Button x:Name="btnCheck" Content="Button" HorizontalAlignment="Left" Margin="156,129,0,0" VerticalAlignment="Top" Width="75" Click="btnCheck_Click"/>
    

    C# Code

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private ObservableCollection<Person> collection = new ObservableCollection<Person>();
    
        public ObservableCollection<Person> Collection
        {
            get { return collection; }
            set { collection = value; OnPropertyChanged(); }
        }
    
        private Person selectedPerson = new Person();
    
        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set { selectedPerson = value; OnPropertyChanged(); }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            collection = new ObservableCollection<Person>() { { new Person("Murray", "Hart")}, { new Person("John", "Clifford") } };
            comboBox.ItemsSource = collection;
        }
    
        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedPerson = (Person)comboBox.SelectedItem;
        }
    
        protected void OnPropertyChanged([CallerMemberName] string name = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    
        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(SelectedPerson.lastName);
        }
    }
    
    public class Person
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
    
        public Person()
        {
    
        }
    
        public Person(string fname, string lname)
        {
            firstName = fname;
            lastName = lname;
        }
    }