簡體   English   中英

如何獲取組合框中所選項目的內容?

[英]how to get the content of selected item in combo box?

我試圖獲取組合框所選項目的值,但它返回此“ 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”

多謝你們! :)

您可以從ComboBox SelectedItem屬性獲取Category

ComboBox SelectedItem是一個對象,因此您需要將其解析為Category

例:

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

您正在使用DataBinding在兩個ComboBox上設置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"/>

您還需要將這兩個屬性: SelectedCategorySelectedSeriesOclist類。

public Category SelectedCategory { get; set; }

public string SelectedSeries { get; set; }

此處閱讀更多有關DataBinding的信息

另外,這是有關WPF ComboBox的出色博客文章。

編輯

如果只想使代碼正常工作,則將其更改為以下內容;

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

但是正確的方法是使用適當的DataBinding方法。

在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); }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM