【问题标题】:how to get the content of selected item in combo box?如何获取组合框中所选项目的内容?
【发布时间】:2019-10-27 05:12:20
【问题描述】:

我正在尝试获取组合框所选项目的值,但它返回此“BakeShop.Category”。

namespace BakeShop
{

class Oclist
{

    public ObservableCollection<Category> Categories { get; set; }
    public Oclist()
    {
        Categories = new ObservableCollection<Category>
        {
            new Category() { Name = "Dry Ingridients", Series = new ObservableCollection<string>()
            { "Flour", "Cake Flour", "Baking Soda" } },

            new Category() { Name = "Wet Ingridients", Series = new ObservableCollection<string>()
            { "Egg", "Coffee liqueur", "Vodka" } },

            new Category() { Name = "Chocolate", Series = new ObservableCollection<string>()
            { "Dark", "Light", "Crushed", "Chips"} }
        };
    }
}
public class Category
{
    public string Name { get; set; }
    public ObservableCollection<string> Series { get; set; }

xaml:

<ComboBox x:Name="CategoryCBox"
                      ItemsSource="{Binding Categories}"
                      DisplayMemberPath="Name"
                      MaxDropDownHeight="100"
                      Height="20" SelectedIndex="0"
                      FontSize="11"/>

<ComboBox x:Name="TypeCBox"
                      ItemsSource="{Binding SelectedItem.Series, ElementName=CategoryCBox}"
                      SelectionChanged="TypeCBox_SelectionChanged"
                      SelectedIndex="0"
                      Height="20"
                      FontSize="11"/>

当我这样做时

string Selected = CategoryCBox.SelectionBoxItem.ToString()
MessageBox.Show(Selected);

它显示“BakeShop.Category”

谢谢大家! :)

【问题讨论】:

  • CategoryCBox 中的项目属于BakeShope.Category 类型,因此当您尝试访问CategoryCBox.SelectedItem 时,它将是该对象,并且执行.ToString() 将为您提供类型名称。您想要做的是将SelectedItem 转换为Category 并访问所需的属性。另外,您是否想从CategoryCBoxTypeCBox 获得价值?
  • 你试过 SelectedValue 吗?
  • @OddmarDam 是的。它具有相同的结果。显示“Bakery.Category
  • @sthotakura 我正在尝试从 CategoryCbox(湿成分、干成分、巧克力)和 TypeCBox(面粉、蛋糕粉、小苏打)系列中获取名称的值,对不起,我是新手我不是很熟悉这些术语..

标签: c# wpf xaml combobox binding


【解决方案1】:

您可以从ComboBoxSelectedItem 属性中获取Category

ComboBoxSelectedItem 是一个对象,因此您需要将其解析为Category

例子:

  1. Category category = yourComboBox.SelectedItem as Category
  2. Category category = (Category)yourComboBox.SelectedItem

【讨论】:

    【解决方案2】:

    您正在使用 DataBinding 在两个 ComboBoxes 上设置 ItemsSource,然后使用 SelectionChanged 事件从它们中获取数据。我认为,正确的方法是 也可以使用 DataBinding 来获取选定的值。

    您的 XAML 如下所示:

    <ComboBox x:Name="CategoryCBox"
              ItemsSource="{Binding Categories}"
              DisplayMemberPath="Name"
              MaxDropDownHeight="100"
              Height="20" 
              SelectedItem="{Binding SelectedCategory}"
              FontSize="11"/>
    
    <ComboBox x:Name="TypeCBox"
              ItemsSource="{Binding SelectedItem.Series, ElementName=CategoryCBox}"
              SelectedItem="{Binding SelectedSeries}"
              Height="20"
              FontSize="11"/>
    

    您还需要将这两个属性:SelectedCategorySelectedSeries 添加到 Oclist 类中。

    public Category SelectedCategory { get; set; }
    
    public string SelectedSeries { get; set; }
    

    在文档here 上阅读有关 DataBinding 的更多信息

    此外,这是 WPF ComboBox 上的出色 blogpost

    编辑

    如果您只是想让您的代码正常工作,请将其更改为以下内容;

    string Selected = ((Category)CategoryCBox.SelectedItem).Name;
    MessageBox.Show(Selected);
    

    但正确的方法是采用正确的DataBinding 方法。

    【讨论】:

    • 我的消息框中仍然出现“BakeShop.Category”:(
    • 如果我在 CategoryCbox 中选择 Dry Ingredients,我的消息框应该显示(“Dry Ingredients”)
    【解决方案3】:

    在 ComboBox 中,创建与所选项目的绑定

    <ComboBox x:Name="CategoryCBox"
                          ItemsSource="{Binding Categories}"
                          DisplayMemberPath="Name"
                          MaxDropDownHeight="100"
                          SelectedItem ="{Binding CategoryCBoxItem}"
                          Height="20" SelectedIndex="0"
                          FontSize="11"/>
    

    然后在你的视图模型中,创建一个绑定属性

    private string categoryCBoxItem;
    public string CategoryCBoxItem
    {
        get { return categoryCBoxItem; }
        set { SetProperty(ref categoryCBoxItem, value); }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2014-02-06
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多