【发布时间】:2018-05-08 04:51:31
【问题描述】:
即将添加以下功能
如果选择此组合框,我想更改数据网格的项目源。
有没有相关的例子?
【问题讨论】:
即将添加以下功能
如果选择此组合框,我想更改数据网格的项目源。
有没有相关的例子?
【问题讨论】:
您可以执行以下操作:
创建一个 WPF 项目。
创建一个包含组合框和数据网格的视图 (xaml)。
为这个新创建的视图创建一个视图模型,并为组合框和网格的 ItemsSource 声明公共属性(集合/列表)。还具有组合框的选定项的属性。
将此视图模型设置为视图的数据上下文。
在组合框选定项的设置器中 - 通过调用方法或您希望的任何方式将绑定到数据网格的 ItemsSource 的属性更改为您的集合。
【讨论】:
我这样做了:
在xaml中为caliburn添加命名空间
xmlns:cal="http://www.caliburnproject.org"
这是组合框:
<ComboBox ItemsSource="{Binding ComboBoxItemSource}" SelectedItem="{Binding SelectedItem}" cal:Message.Attach="[Event SelectionChanged] = [ComboBoxSelectionChanged()]" />
并且视图模型应该有这个方法:
public void ComboBoxSelectionChanged()
{
// here based on the SelectedItem you can change the ItemSource for the dataGrid.
}
每当您更改 Combobox 的 selectedItem 时,该方法都会受到影响,根据您需要的逻辑,您可以为 dataGrid 分配 ItemSource。
希望这会有所帮助:)
【讨论】: