[WPF学习笔记] - ComboBox的一些应用实现

默认显示数据第一项

代码实现

comboBox.ItemsSource = Data;
comboBox.DisplayMemberPath = "Key"; // if data is dict
comboBox.SelectedValuePath = "Value"; // if data is dict
comboBox.SelectedItem = comboBox.Items[0];

XAML实现

 <ComboBox
     DisplayMemberPath="Key"
     SelectedValuePath="Value"
     ItemsSource="{Binding Data}" 
     IsSynchronizedWithCurrentItem="True" # important
     />

C# WPF Combobox select first item

多个ComboBox级联

以2个ComboBox为例,完全用xaml实现。
假设Data数据类型为Dictionary>

<ComboBox
	name="cb1"
    DisplayMemberPath="Key"
    SelectedValuePath="Value"
    ItemsSource="{Binding Data}" 
    IsSynchronizedWithCurrentItem="True"
    />
    
<ComboBox
	name="cb2"
    ItemsSource="{Binding ElementName=cb1, Path=SelectedValue}"
    IsSynchronizedWithCurrentItem="True"
    />

你可能感兴趣的:(C#/.NET,WPF,学习笔记)