将ComboBox的SelectedItem设置为ListBox UWP中的对象

时间:2018-09-13 10:46:49

标签: c# 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;
    }
}

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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