首页 > 解决方案 > 将 ComboBox 的 SelectedItem 设置为 ListBox UWP 中的对象

问题描述

我有一个列表视图填充我的会计科目表:

public class ChartOfAccounts
{
    public int AccountCode { get; set; }
    public string AccountTitle { get; set; }
    public string Description { get; set; }
    public string SubCategory { get; set; }
    public string Category { get; set; }
    public bool Active { get; set; }
}

通过此列表视图,我想填充其他控件,例如:

private void MainRadDataGrid_SelectionChanged(object sender, Telerik.UI.Xaml.Controls.Grid.DataGridSelectionChangedEventArgs e)
{
    RadDataGrid rdg = (RadDataGrid)sender;

    var SelectedCOA = (ChartOfAccounts)rdg.SelectedItem;

    if (rdg !=null && rdg.SelectedItems.Count > 0) {
        AccountCodeTextBox.Text = SelectedCOA.AccountCode.ToString();
        AccountTitleTextBox.Text = SelectedCOA.AccountTitle;
        DescriptionTextBox.Text = SelectedCOA.Description;
        CategoryComboBox.SelectedItem = SelectedCOA.Category;

        SubCategoryComboBox.SelectedItem = SelectedCOA.SubCategory;
    }
}

问题是,我无法将CategoryandSubCategory组合框设置为相关的Categoryand SubCategory。ComboBox 只显示CategorySub Category单词,而不是实际选择的项目。

谁能解释为什么这不起作用?

标签: c#uwp

解决方案


我认为你的答案是这样的:

CategoryComboBox.SelectedItem = Combox1.FindStringExact(SelectedCOA.Category.?) // ? = displayed cat name

或者

CategoryComboBox.SelectedIndex = CategoryComboBox.Items.IndexOf(SelectedCOA.Category.?);

推荐阅读