开发者

WPF Combobox binding Question

I have a 2 Table.

Product

Product开发者_如何学GoName

CategoryID

Category

ID

CategoryName

I'm filling combobox to table named 'category'.

Code

Product currentProduct=datacontext.products.FirstOrDefault();
this.datacontext=currentProduct;
combobox1.Itemssource=datacontext.categories;

XAML

<Textbox Text="{Binding Path=ProductName}"></Textbox>
<Combobox x:Name="combobox1" SelectedItem="Binding Path=CategoryID"></Combobox>

When click save button, I'm doing datacontext.SubmitChanges()

ProductName changed. But CategoryID not changed.

My target is when i select from combobox, selected category ID set to CategoryID of currentProduct. (like currentProduct.CategoryID=(Category as combobox1.SelectedItem).ID)

How to do is from xaml?


Bind the SelectedValue property of the ComboBox to the CategoryID of the product, not the SelectedItem. You also need to set the SelectedValuePath property :

<Combobox x:Name="combobox1" SelectedValuePath=CategoryID, SelectedValue="{Binding Path=CategoryID, Mode=TwoWay}"></Combobox>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜