Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
WPF ComboBox doesn't ever update text to SelectedItem's property, though SelectedItem changes on selection.

XAML:
XML
<ComboBox  Margin="4 0 2 0"
           ItemsSource="{Binding XAxes}"
           SelectedItem="{Binding SelectedXAxis, Mode=TwoWay}"
           ItemTemplate="{StaticResource AxisCBTextTemplate}"/>

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


VM:
C#
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:

XML
<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
XML
<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.
[no name] 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

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900