Click here to Skip to main content
14,494,314 members
Rate this:
Please Sign up or sign in to vote.
See more:
WPF ComboBox doesn't ever update text to SelectedItem's property, though SelectedItem changes on selection.

XAML:
<ComboBox  Margin="4 0 2 0"

           ItemsSource="{Binding XAxes}"

           SelectedItem="{Binding SelectedXAxis, Mode=TwoWay}"

           ItemTemplate="{StaticResource AxisCBTextTemplate}"/>

Template is:
<DataTemplate x:Key="AxisCBTextTemplate">
    <TextBlock Text="{Binding AxisTitle}"/>
</DataTemplate>


VM:
private AxisCollection _xAxes;
public AxisCollection XAxes
{
    get => _xAxes;
    private set
    {
        _xAxes = value;
        OnPropertyChanged("XAxes");
    }
}

private IAxis _selectedXAxis;
public IAxis SelectedXAxis
{
    get => _selectedXAxis;
    set
    {
        _selectedXAxis = value;
        OnPropertyChanged(nameof(SelectedXAxis));
    }
}

SelectedXAxis setter gets called, as well as OnPropertyChanged, but ComboBox does not show any text. List is shown as intended, no AxisTitle is lost.

What I have tried:

Changing ItemTemplate to DisplayMemberPath and SelectedValuePath:

<ComboBox  Margin="4 0 2 0"

    ItemsSource="{Binding YAxes}"

    SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"

    DisplayMemberPath="AxisTitle"

    SelectedValuePath="AxisTitle"/>


Adding an OnPropertyChanged("AxisTitle") to a VM code;
Different combinations of ItemTemplate, DisplayMemberPath and SelectedValuePath;
Leaving only
<ComboBox  Margin="4 0 2 0"

    ItemsSource="{Binding YAxes}"/>
causes an exception.
Posted
Updated 28-Mar-20 2:32am
Comments
Richard Deeming 26-Mar-20 8:01am
   
Check the output window in Visual Studio to see if there are any data binding errors.
Debugging data bindings - The complete WPF tutorial[^]
Arli Chokoev (Choke) 26-Mar-20 8:02am
   
There are none.
Gerry Schmitz 26-Mar-20 11:01am
   
Did you implement INotifyPropertyChanged? Did you set the DataContext?
Arli Chokoev (Choke) 26-Mar-20 12:41pm
   
Yes and yes. As told, setter for the SelectedItem fires, as well as OnPropertyChanged, but ComboBox text does not change.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

In your XAML for the combobox, try adding IsSynchronizedWithCurrentItem="True".

Also, why are you using a data template? Just bind to the desired property in your object and be done with it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100