ListBox的使用
通过添加多个Item实现向ListBox控件添加更多数据,这些Item不仅可以是文本数据,还可以是其他控件元素。ListBox控件常用的事件为SelectionChanged。
- private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ListBoxItem selectedItem = listBox1.SelectedItem as ListBoxItem;
- textBlock1.Text = "您选择了:" + selectedItem.Content.ToString();
- }
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <StackPanel>
- <ListBox Name="listBox1" SelectionChanged="ListBox_SelectionChanged">
- <ListBoxItem>第一项</ListBoxItem>
- <ListBoxItem>第二项</ListBoxItem>
- <ListBoxItem>第三项</ListBoxItem>
- <ListBoxItem>第四项</ListBoxItem>
- </ListBox>
- </StackPanel>
- <TextBlock Height="50" Width="200" HorizontalAlignment="Left" Name="textBlock1"></TextBlock>
- </Grid>