WPF 列表框 - 更改数据模板中控件的项目源
我有一个带有数据模板的列表框,其中包含绑定到我的集合的许多控件。
我想要的是根据同一行中其他组合框之一中选择的值来更改这些组合框之一的项目源。我不想更改列表框中其余行中所有相应组合框的项目源。
如何仅获取所选行中的控件的句柄。
使用 WPF 数据网格是否可以更轻松地尝试执行此操作?
谢谢。
I have a listbox with a datatemplate that holds a number of controls bound to my collection.
What i want is to change the itemsource for one of these comboboxes dependant upon the value selected in one of the other comboboxes in the same row. I don't want to change the itemsource for all corresponding comboboxes within the rest of the rows in the listbox.
How do I get a handle on the control in the selected row only.
Is this something that is easier to try doing witht the WPF datagrid?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ListBox 实际上更容易,因为 DataTemplate 定义了一行的所有控件。
我认为最简单的方法是在绑定上使用转换器。您将把第二个 ComboBox 的 ItemsSource 绑定到第一个 ComboBox 的 SelectedItem:
操作轻松传入 DataContext:
请注意,如果您需要来自 Row 的 DataContext 的附加信息,您可以将其设为 MultiBinding 和 IMultiValueConverter,并通过执行以下 在您的转换器类中,执行您必须执行的操作以获得正确的项目源。
This is actually easier with the ListBox, as the DataTemplate defines all the controls for a row.
I think the easiest way is to use a converter on a binding. You will bind your second ComboBox's ItemsSource to the SelectedItem of the first ComboBox:
Note that if you need additional information from the DataContext of the Row, you can make it a MultiBinding and an IMultiValueConverter, and pass in the DataContext easily by doing:
Then, in your converter class, do whatever it is you have to do in order to get the correct items source.
获取该特定组合框的 SelectionChanged 事件,并在该事件内设置其他组合框的 Itemsource。
另外,最好获取 listview 的 SelectionChaged 事件并处理它。
Get the SelectionChanged event of that paticular combobox and set the Itemsource of the other combobox inside the event.
Also it is better to get the SelectionChaged event of listview and handle it.