【问题标题】:Binding Command in Datagrid with Prism WPF使用 Prism WPF 在 Datagrid 中绑定命令
【发布时间】:2012-10-31 14:42:58
【问题描述】:

我在谷歌上搜索了我的问题,但找不到任何可以解决我问题的答案。 我试图从 WPF 中我的数据网格内的按钮绑定命令。我使用 Prism 来处理 MVVM。 这是我绑定命令的代码:

<DataGrid AutoGenerateColumns="False" 
              ...
              SelectedItem="{Binding OrderDetail}"
              ItemsSource="{Binding ListOrderDetail}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Deliver Order" 
                                Command="{Binding Path=DataContext.DeliverOrderCommand}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

这是我的视图模型,其中包含命令功能:

public ICommand DeliverOrderCommand
    {
        get 
        {
            if (deliverOrderCommand == null)
                deliverOrderCommand = new DelegateCommand(DeliverOrderFunc);
            return deliverOrderCommand; 
        }
        set { deliverOrderCommand = value; }
    }

当我尝试调试时,它没有进入 ICommand。 那么如何将数据网格中的按钮绑定到我的视图模型?

【问题讨论】:

    标签: wpf mvvm datagrid command prism


    【解决方案1】:

    您的问题是因为 DataColumns 不是可视化树的一部分,因此不继承 DataGrid 的 DataContext。

    一种可能克服此问题的方法是使用绑定指定祖先:

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="Deliver Order" 
                    Command="{Binding  Path=DataContext.DeliverPesananCommand
                                      ,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
                    />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    

    另一种(有点笨拙)的方法是声明一个帮助类,它为 DataGridColumn 类创建一个附加属性,然后在网格的数据上下文发生更改时填充该属性(它通过处理在 FrameworkElement 级别更改的事件和检查负责事件的依赖对象是否为 DataGrid):

    public class DataGridContextHelper
    {
    
        static DataGridContextHelper()
        {
            DependencyProperty dp = FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridColumn));
            FrameworkElement.DataContextProperty.OverrideMetadata( typeof(DataGrid)
                                                                  ,new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataContextChanged)
                                                                 );
        }
    
        public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var grid = d as DataGrid;
            if (grid == null) return;
    
            foreach (var col in grid.Columns)
            {
                col.SetValue(FrameworkElement.DataContextProperty, e.NewValue);
            }
        }
    }
    

    您可以找到有关此方法的更多详细信息here

    【讨论】:

    • 感谢@slugster 我的问题使用第一种方法解决了。 :D
    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 2015-05-08
    • 2011-03-12
    • 2021-08-29
    • 1970-01-01
    • 2017-02-20
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多