0

Here is the code of XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ComboBox x:Name="CB" SelectedValue="{Binding Model,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Value" VerticalAlignment="Center">
            
        </ComboBox>
    </Grid>
</Window>

And here is the code of code-behind:

public partial class MainWindow : Window
    {
        public List<TestModel> Models { get; set; } = new List<TestModel>();
        TestModel _Model = new TestModel() { Key = "Joe", Value = "456" };        

        public TestModel Model
        {
            get => _Model; set
            {
                if (_Model != value)
                {
                    _Model = value;                    
                }
            }
        }
       
        public MainWindow()
        {
            InitializeComponent();
            Models.Add(new TestModel() { Key = "John", Value = "123" });
            Models.Add(new TestModel() { Key = "Joe", Value = "456" });
            Models.Add(new TestModel() { Key = "Kay", Value = "547" });
            Models.Add(new TestModel() { Key = "Rose", Value = "258" });
            CB.ItemsSource = Models;
            this.DataContext = this;
        }
        public class TestModel
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }
    }

I bind the SelectedValue to the Model which is already existed in the List. but the selection is still blank. enter image description here

What's wrong with my code? I need the combobox select the item correctly.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Volfogg
  • 7
  • 4
  • The model instance assigned to Model property is **not** a member of Models collection. In fact you have **two** instances with the same property values but they are not the same as having two red apples – Sir Rufo Feb 06 '23 at 08:55
  • See also [Difference between SelectedItem, SelectedValue and SelectedValuePath](https://stackoverflow.com/q/4902039/1136211). – Clemens Feb 06 '23 at 09:02
  • 1
    In order to select an item from the ItemsSource collection, the object passed to SelectedItem (or SelectedValue without SelectedValuePath) must compare equal to the ItemsSource element. Either it is an element of the source collection, or it has an appropriate Equals method override. – Clemens Feb 06 '23 at 09:04
  • Also note that the Model property should fire a change notification, so that the UI is automatically updated when the property is changed from somewhere else. – Clemens Feb 06 '23 at 09:06

2 Answers2

0

Model should return one of the items from Models.

Add this to the constructor before DataContext setter:

Model = Models[1];

The problematic line is _Model = new TestModel() { Key = "Joe", Value = "456" } - you create an instance of TestModel that is not present in the list. Even though it has the same property values, it is not the same object.

Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
0

ComboBoxes use the .Equals() method of the objects they are displaying to find the SelectedItem in the ItemsSource.

Since the Equals() method for clasess by default works by comparing object references, you would need to set the SelectedItem to an exact same instance as an object inside the ItemsSource.

Another approach you could take is overriding the Equals() method on your object and making it Equal by value comparison or use a record class which automatically makes your object comparable by value.

Selmir Aljic
  • 268
  • 2
  • 9