C# 获取 wpf 组合框选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19454008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:01:55  来源:igfitidea点击:

Get wpf combobox selected value

c#wpf

提问by user3357963

How do I get the selected value (eg Option1) as a stringfrom my example below. I've tried loads of suggestions on Google but can't get the string.

我如何从下面的示例中获取选定的值(例如Option1string。我在 Google 上尝试了大量建议,但无法获取字符串。

XAML:

XAML:

<ComboBox x:Name="selectOption" Text="Select Option" 
                 SelectionChanged="selectOption_SelectionChanged" 
                 SelectedValue="{Binding VMselectedOption, Mode=TwoWay}" >
    <ComboBoxItem Name="cbb1">Option1</ComboBoxItem>
    <ComboBoxItem Name="cbb2">Option2</ComboBoxItem>
    <ComboBoxItem Name="cbb3">Option3</ComboBoxItem>
</ComboBox>

codebehind:

代码隐藏:

private void selectOption_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   var selectedValue = selectOption.SelectedValue; 
}

//elsewhere in code
var test = viewModel.VMselectedOption;

Both selectedValue and test return the string "System.Windows.Controls.ComboBoxItem: Option1" and not "Option1"

selectedValue 和 test 都返回字符串“ System.Windows.Controls.ComboBoxItem: Option1”而不是“ Option1

This should be really simple but I just cannot get this working or see what is wrong?

这应该很简单,但我无法让它正常工作或看看有什么问题?

采纳答案by Vlad

You shouldn't insert the combobox items manually. Set them by using ItemsSource.

您不应手动插入组合框项目。使用ItemsSource.

Basically you should create a list of options (or objects representing options) and set them as ItemsSource, this way your SelectedItemwill be exactly the option which is selected, not the automatically created wrapping ComboboxItem.

基本上,您应该创建一个选项列表(或代表选项的对象)并将它们设置为ItemsSource,这样您SelectedItem将完全是选择的选项,而不是自动创建的 wrapping ComboboxItem

回答by Daniel

ComboBoxItem.Contentis of type Object, so you'll need to cast the item yourself.

ComboBoxItem.Content是 Object 类型,因此您需要自己投射该项目。

回答by Nitin

Update your code to get the Content of comboboxItem.

更新您的代码以获取 comboboxItem 的内容。

var selectedValue = ((ComboBoxItem)selectOption.SelectedItem).Content.ToString();

回答by Anatoly Nikolaev

string Value="";
if(myComboBox.SelectedIndex>=0) 
  Value=((ComboBoxItem)myComboBox.SelectedItem).Content.ToString();

回答by Supitchaya

You should set SelectedValuePath="Content".

您应该设置 SelectedValuePath="Content"。

<ComboBox x:Name="selectOption" Text="Select Option" 
                 SelectionChanged="selectOption_SelectionChanged" 
                 SelectedValue="{Binding VMselectedOption, Mode=TwoWay}" 
                 SelectedValuePath="Content">
    <ComboBoxItem Name="cbb1">Option1</ComboBoxItem>
    <ComboBoxItem Name="cbb2">Option2</ComboBoxItem>
    <ComboBoxItem Name="cbb3">Option3</ComboBoxItem>
</ComboBox>