开发者

Bind Silverlight listbox to a List<>

I want to bind a Silverlight ListBox to a List<Products> collection - ProductName. I want to show the Produc开发者_JAVA技巧tName when the user selects an item in the Listbox. How can I do it?


Try the following:

<ListBox x:Name="lbProductList"
         ItemsSource="{Binding}" 
         DisplayMemberPath="ProductName" 
         SelectedValuePath="ProductId"
          />

Set the datacontext of the listbox to the productlist:

lbProductList.DataContext = myProductList;

Event better would be to define a ViewModel with the productList and the selected product. The you could bind the SelectedValue as well:

SelectedValue="{Binding SelectedProduct, Mode=TwoWay}"

To display the selected Product you could do with element binding:

<TextBlock Text="{Binding SelectedItem.ProductName, ElementName=lbProductList}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜