开发者

Bind Silverlight listbox to a List<>

开发者 https://www.devze.com 2023-02-07 14:28 出处:网络
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

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

精彩评论

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