简体   繁体   English

将组合框绑定到自定义列表Windows Phone

[英]Binding Combobox to custom list windows phone

I have a custom list with structure category -id -name -color I create a list and bind it to combobox name categoryListBox in my xaml page. 我有一个结构类别为-id -name -color的自定义列表,我创建了一个列表并将其绑定到我的xaml页面中的组合框名称categoryListBox上。

I have tried using this piece of code 我试过使用这段代码

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

using this as resource 使用它作为资源

Whenever I run the app, all I get is a blank screen, although when I get collection of items using 每当我运行该应用程序时,我得到的只是一个空白屏幕,尽管当我使用

 var item = categoryListBox.Items;

it shows it of type System.Generic.Lists having the number of items it should have. 它显示类型为System.Generic.Lists的项目,其中应包含该项目的数量。 I seem to be on a standstill here, and don't know whats wrong with it. 我似乎在这里停滞不前,不知道这是怎么回事。 I even looked at an example , but couldn't understand much. 我什至看了一个例子 ,但听不懂。

This is the definition of combobox 这是combobox的定义

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

And here is the file for category class. 这是类别类的文件。

the values in DisplayMemberPath and SelectedValuePath properties must be a property name (not variable), and accessible ( public modifier). DisplayMemberPathSelectedValuePath属性中的值必须是属性名称(不是变量),并且可以访问public修饰符)。

so if you edit your category class, by replacing this: 因此,如果您通过替换以下内容来编辑category类:

int id;
string name;
int color;

with: 与:

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

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM