【问题标题】:Binding Combobox to custom list windows phone将 Combobox 绑定到自定义列表 windows phone
【发布时间】:2015-07-27 16:50:01
【问题描述】:

我有一个带有结构的自定义列表 类别 -ID -姓名 -颜色 我创建了一个列表并将其绑定到我的 xaml 页面中的组合框名称 categoryListBox。

我尝试过使用这段代码

List<category> categoryCollection = await       categoryList.GetCategoryListAsync();
categoryListBox.ItemsSource = categoryCollection;
categoryListBox.DisplayMemberPath = "name";
categoryListBox.SelectedValuePath = "id";

使用this as resource

每当我运行应用程序时,我得到的只是一个空白屏幕,尽管当我使用收集项目时

 var item = categoryListBox.Items;

它显示它的类型 System.Generic.Lists 具有它应该具有的项目数。我似乎在这里停滞不前,不知道有什么问题。我什至看了example,但看不懂。

这是组合框的定义

 <ComboBox 
     Canvas.ZIndex="100"
     Name="categoryListBox"
     Foreground="Black"
     Margin="10,0"
     PlaceholderText="Select.."  
     Style="{StaticResource ComboBoxStyle1}"/>

And here is the file for category class.

【问题讨论】:

    标签: c# xaml data-binding combobox windows-phone-8.1


    【解决方案1】:

    DisplayMemberPathSelectedValuePath 属性中的值必须是属性 名称(非变量)和可访问public 修饰符)。

    所以如果你编辑你的 category 类,通过替换这个:

    int id;
    string name;
    int color;
    

    与:

    public int id { get; set; }
    public string name { get; set; }
    public int color { get; set; }
    

    它应该可以工作。

    【讨论】:

    • 没有public 限定符,这不应该只使用{get; set;},因为C# 默认为internal 并且应该仍然可以访问?
    • 没有。必须是公开的。可能是因为控制的逻辑是一个外部程序集。
    猜你喜欢
    • 2010-10-08
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多