1.ComboBox空间
练习
1.创建一个wpf项目,添加一个usercontrol项(name:ComboBoxUserControl)。
ComboBoxUserControl.xaml核心代码
<Grid>
<StackPanel>
<TextBlock>学生列表:</TextBlock>
<ComboBox>
<ComboBoxItem>
<TextBlock>张三</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock>李四</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock>王五</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock>赵六</TextBlock>
</ComboBoxItem>
</ComboBox>
</StackPanel>
</Grid>
2.在MainWindow.xaml引入
xmlns:local="clr-namespace:ComboBox"
<Grid>
<local:ComboBoxUserControl></local:ComboBoxUserControl>
</Grid>
3.当给Combox添加SelectedIndex时,就会出现初始值。如:
<ComboBox SelectedIndex="2">
4.显示选中的值。如:
<ComboBox SelectionChanged="ComboBox_SelectionChanged">
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cb = (ComboBox)sender;
var selectedItem = cb.SelectedItem;
var selectecValue = cb.SelectedValue;
MessageBox.Show(string.Format("selectedItem={0},selectedValue={1}",selectedItem,selectecValue));
}
网友评论