wpf Combobox Itemssource在Listbox数据模板中没有绑定 [英] wpf Combobox Itemssource not binding within Listbox Data Template
本文介绍了wpf Combobox Itemssource在Listbox数据模板中没有绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助解决以下问题。我在列表框项目datatemplate中有Combobox,这样我就可以加载多个组合框但是相同的项目源
Please help to fix the below issue. I have Combobox inside listbox item datatemplate so that I can load multiple combobox but same itemsource
<StackPanel>
<ListBox Width="400" Name="lstFiles" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="dataGrid">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Name="dragFileName"
Margin="5,0,0,0"
Text="{Binding fileName, UpdateSourceTrigger=Default}" />
<ComboBox Grid.Row="0"
Grid.Column="1"
Margin="5,0,0,0"
Name="cboDragDocType"
ItemsSource="{Binding dragDocType, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
Text="{Binding dragDocTypeText}"
IsEditable="True"
IsReadOnly="True" />
<Button Name="dragDelBtn"
Grid.Column="2"
Height="20">X</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Data is loading in comboBox by using ObservableCollection property. In Initialize method i do for loop method to get multiple items. Problem scenario: Select item from the dropdown1 --> then Select item from the dropdown2 -->Again click dropdown1, Now dropdown1 itemssource is empty.
推荐答案
有一些奇怪的行为一个约束迷路了。
我遇到了类似于组合框和文本框的东西,Textbox.Text绑定到DependencyProperty的ID属性,而DependencyProperty又绑定到组合框的SelectedItem;用户可以输入id,反向选择函数会更新绑定到ComboBox.SelectedItem的属性(不直接到ComboBox.SelectedItem,预计会破坏绑定)。
最后,我存储了BindingExpression,并在ComboBox_DropDownOpened中重新应用它。
不是一个漂亮的解决方案,更多的是WTF,但是它有效。
也许这样的解决办法可以对你的情况也很有帮助。
There is some strange behavior of a binding getting lost.
I experienced something similar with a combination of a combobox and a textbox, with Textbox.Text bound to the ID property of a DependencyProperty which is in turn bound to the SelectedItem of the combobox; the user could type the id and a reverse selection function would update that property which is bound to ComboBox.SelectedItem (not to ComboBox.SelectedItem directly, that would be expected to break the binding).
Eventually, I stored the BindingExpression, and re-applied it in ComboBox_DropDownOpened.
Not a beautiful solution, more of a WTF, but it works.
Perhaps such a work-around could be helpful in your case, too.
这篇关于wpf Combobox Itemssource在Listbox数据模板中没有绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文