WPF ComboBox binding

Arli Chokoev 1 Reputation point
2020-03-18T11:34:37.233+00:00

How can I make a ComboBox to display the selected item?

I have following ComboBox declared in XAML:

<ComboBox Margin="4 0 2 0";
ItemsSource="{Binding YAxes}"
SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
DisplayMemberPath="AxisTitle"
SelectedValuePath="AxisTitle"/>

Code behind:

private AxisCollection _yAxes;
public AxisCollection YAxes
{
    get => _yAxes;
    private set
    {
        _yAxes = value;
        OnPropertyChanged("YAxes");
    }
}

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

On items selection setter on SelectedYAxis fires, but ComboBox does never update its text.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-03-18T17:08:34.323+00:00

    HI,
    without knowing your code it's impossible to solve your problem. Try following demo. Select line in upper ComboBox change the displaying Text in Label and update the lower ComboBox.

    XAML

    <Window x:Class="Window90"
            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="Window90" Height="450" Width="800">
      <Window.DataContext>
        <local:Window90VM/>
      </Window.DataContext>
      <StackPanel>
        <ComboBox  Margin="4 0 2 0"
                                ItemsSource="{Binding YAxes}"
                                SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
                                DisplayMemberPath="AxisTitle"
                                SelectedValuePath="AxisTitle"/>
        <Label Content="{Binding SelectedYAxis.AxisTitle}" Height="100"/>
        <ComboBox  Margin="4 0 2 0"
                                ItemsSource="{Binding YAxes}"
                                SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
                                DisplayMemberPath="AxisTitle"
                                SelectedValuePath="AxisTitle"/>
      </StackPanel>
    </Window>
    

    And ViewModel and data class:

    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class Window90VM
      Implements INotifyPropertyChanged
    
      Public Sub New()
        For i = 1 To 10
          col.Add(New Data With {.AxisTitle = $"Titel {[i]}"})
        Next
        cvs.Source = col
      End Sub
    
      Private cvs As New CollectionViewSource
      Private col As New ObservableCollection(Of Data)
    
      Public ReadOnly Property YAxes As ICollectionView
        Get
          Return cvs.View
        End Get
      End Property
    
      Private _selectedYAxis As Data
      Public Property SelectedYAxis As Data
        Get
          Return Me._selectedYAxis
        End Get
        Set(value As Data)
          Me._selectedYAxis = value
          OnPropertyChanged()
        End Set
      End Property
    
    #Region " PropertyChanged"
      Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
      Friend Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
      End Sub
    #End Region
    
      Public Class Data
        Public Property AxisTitle As String
      End Class
    
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.