开发者

WPF, Binding a combobox and textbox to each other AND data source

开发者 https://www.devze.com 2023-03-02 15:26 出处:网络
I have a two column combobox, and a textbox, bound to xml data. The textbox shows the equivalent of the comboboxes second column of the currently selected item.

I have a two column combobox, and a textbox, bound to xml data.

The textbox shows the equivalent of the comboboxes second column of the currently selected item.

I've bound the datacontext of the textbox to the SelectedItem in the combobox, which t开发者_开发百科hen updates if you select a row in the combobox. Now, I'd it so that if you type something into the textbox that corresponds to a value in the 2nd column of the combobox, it selects that row.

I realise this is slightly circular.

I've managed it before in winforms, by effectively suspending events when the CombobBox OnSelectedItemChanged fires and updates the textbox or OnTextChange fires and updates selectedItem.

The idea is that the user can either select an option from the combo, or if they know a short code (in this case, country ISO), they can just type it in and immediately see the appropriate country selected in the combobox.

Is it somehow possible to bind the selectedItem in the combobox to the textBox in addition to the underlying data (and indeed does that idea make any sense?), or possibly do some sort of two-way-binding between these elements?

I'm hoping there's a simpler solution than dependencyproperties-- ideally something purely in xaml, but appreciate as I'm new at WPF, I've no idea if this is even possible.

Thanks! Mike


If you've got a ViewModel you can two-way bind each to the same property of that, and this is probably the best way to do it.

0

精彩评论

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

关注公众号