user3621898 user3621898 - 1 month ago 8
C# Question

SelectionChanged isn't firing when user changes combobox in WPF

So my

selectionChanged
event isn't firing when a user changes it but fires if I change it in code.

I'm using the
selectionchanged
to get a selection from a combobox to fill another combobox.



<!--1st list -->
<ComboBox ItemsSource="{Binding djurKatList}" SelectedItem="{Binding SelectedDjurKat}" SelectionChanged="listKat_SelectionChanged" x:Name="listKat" HorizontalAlignment="Left" Margin="98,118,0,0" VerticalAlignment="Top" Width="120"/>

<!--2nd list that's filled after something is selected in 1st list-->
<ComboBox ItemsSource="{Binding djurArtList}" SelectedItem="{Binding SelectedDjurArt}" x:Name="listDjur" HorizontalAlignment="Left" Margin="98,154,0,0" VerticalAlignment="Top" Width="120"/>





In code behind this fills the 2nd combobox and isn't executing when I change the 1st combobox:

private void listKat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fyllDjurArtLista(hanterare, typeof(Daggdjur));
}


When I change the selection in code,
selectionChanged
fires, here below I change my selected
comboboxitem
, the event is fired and 2nd combobox gets filled.

var listItem = new ComboBoxItem { Content = "Välj kategori" };
SelectedDjurKat = listItem;

Answer

Here is simple example and it works.

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();

        VM v = new VM();
        v.djurKatList = new List<string>();
        v.djurKatList.Add("1");
        v.djurKatList.Add("2");
        v.djurKatList.Add("3");
        v.djurKatList.Add("4");
        v.djurKatList.Add("5");

        DataContext = v;
    }

    private void listKat_SelectionChanged(object sender, SelectionChangedEventArgs e) {
       // fyllDjurArtLista(hanterare, typeof(Daggdjur));
    }
}

public class VM {
    public List<string> djurKatList { get; set; }
    public string SelectedDjurKat { get; set; }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <!--1st list -->
        <ComboBox ItemsSource="{Binding djurKatList}" SelectedItem="{Binding SelectedDjurKat}" SelectionChanged="listKat_SelectionChanged"
                  x:Name="listKat" HorizontalAlignment="Left" Margin="98,118,0,0" VerticalAlignment="Top" Width="120"/>

        <!--2nd list that's filled after something is selected in 1st list-->
        <ComboBox ItemsSource="{Binding djurArtList}" SelectedItem="{Binding SelectedDjurArt}" x:Name="listDjur" HorizontalAlignment="Left"
                  Margin="98,154,0,0" VerticalAlignment="Top" Width="120"/>
    </StackPanel>
</Grid>

The SelectionChanged is fired. But if the problem is that the other combo box is not filled then you need to use INotifyPropertyChanged as suggested. You need to call PropertyChanged for the property djurArtList.

Comments