[英]ComboBox SelectionChanged event firing when we changing value of combobox WPF
[英]SelectionChanged isn't firing when user changes combobox in WPF
因此,我的selectionChanged
事件在用戶更改時不會觸發,但在代碼中更改時會觸發。
我正在使用selectionchanged
來從組合框獲取選擇內容以填充另一個組合框。
<!--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"/>
在此代碼后面的代碼填充了第二個組合框,並且在我更改第一個組合框時未執行:
private void listKat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fyllDjurArtLista(hanterare, typeof(Daggdjur));
}
當我在代碼中更改選擇時,將觸發selectionChanged
,在此下面,我將更改所選的comboboxitem
,將觸發事件並填充第二個combobox。
var listItem = new ComboBoxItem { Content = "Välj kategori" };
SelectedDjurKat = listItem;
這是一個簡單的示例,它可以工作。
/// <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>
將觸發SelectionChanged。 但是,如果問題是未填充另一個組合框,則需要按照建議使用INotifyPropertyChanged 。 您需要為屬性djurArtList調用PropertyChanged。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.