wpf输出下拉选择框选择的数据
时间: 2024-04-13 18:21:13 浏览: 10
以下是WPF中输出下拉选择框选择的数据的示例代码:
```xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox x:Name="comboBox" Width="200" Height="30" SelectedIndex="0">
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
</ComboBox>
<Button Content="Get Selected Item" Width="150" Height="30" Click="Button_Click"/>
</Grid>
</Window>
```
```csharp
private void Button_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem selectedItem = comboBox.SelectedItem as ComboBoxItem;
if (selectedItem != null)
{
MessageBox.Show(selectedItem.Content.ToString());
}
}
```
在上述示例中,我们创建了一个ComboBox控件,并添加了三个ComboBoxItem作为下拉列表中的选项。当用户选择其中一个选项时,单击按钮将会弹出一个消息框,显示所选项的内容。