【问题标题】:using WPF Caliburn, How do I change datagrid binding based on the combobox selection?使用 WPF Caliburn,如何根据组合框选择更改数据网格绑定?
【发布时间】:2018-05-08 04:51:31
【问题描述】:

即将添加以下功能

如果选择此组合框,我想更改数据网格的项目源。

有没有相关的例子?

【问题讨论】:

    标签: wpf mvvm caliburn


    【解决方案1】:

    您可以执行以下操作:

    1. 创建一个 WPF 项目。

    2. 创建一个包含组合框和数据网格的视图 (xaml)。

    3. 为这个新创建的视图创建一个视图模型,并为组合框和网格的 ItemsSource 声明公共属性(集合/列表)。还具有组合框的选定项的属性。

    4. 将此视图模型设置为视图的数据上下文。

    5. 在组合框选定项的设置器中 - 通过调用方法或您希望的任何方式将绑定到数据网格的 ItemsSource 的属性更改为您的集合。

    【讨论】:

      【解决方案2】:

      我这样做了:

      在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。

      希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2014-01-08
        • 2017-05-17
        • 2021-09-18
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        • 2013-03-19
        相关资源
        最近更新 更多