简体   繁体   中英

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.

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. 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 
     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).

so if you edit your category class, by replacing this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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