DataGrid.ColumnHeaderStyle和命令绑定 [英] DataGrid.ColumnHeaderStyle and Command Binding

查看:138
本文介绍了DataGrid.ColumnHeaderStyle和命令绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的XAML中,命令属性只能在列B中正确工作。单击列A的头不会调用命令的execute方法。不同的是,在列B中,DataGridColumnHeader被显式实例化。

In the XAML below, the command property is correctly working only with Column B. Clicking on Column A's header is not invoking command's execute method. The difference is that in Column B, DataGridColumnHeader is instantiated explicitly.

另一方面,第二部分风格,设置背景基于压缩状态的触发器正在为两列。

On other hand, the second part of style, the trigger that sets background base on Pressed state is working for both columns.

为什么Command属性可以与列B&不是列A?

Why does the Command property work with Column B & not with Column A ?

<DataGrid x:Name="Test" 
                  ItemsSource="{Binding Items}" 
                  AutoGenerateColumns="False" >

            <DataGrid.ColumnHeaderStyle>

                <Style TargetType="{x:Type DataGridColumnHeader}">

                    <Setter Property="Command" 
                            Value="{Binding MyCommand}"/>
                    <Setter Property="CommandParameter" 
                            Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/>

                    <Style.Triggers>

                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="Red" />
                        </Trigger>

                    </Style.Triggers>

                </Style>

            </DataGrid.ColumnHeaderStyle>

            <DataGrid.Columns>

                <DataGridTextColumn FontSize="12" 
                                    Header="Column A" 
                                    Width="200" 
                                    Binding="{Binding AData}" />

                <DataGridTextColumn FontSize="12" 
                                    Width="200" 
                                    Binding="{Binding BData}">

                    <DataGridTextColumn.Header>
                        <DataGridColumnHeader Content="Column B" />                                            
                    </DataGridTextColumn.Header
                        >
                </DataGridTextColumn>

            </DataGrid.Columns>

        </DataGrid>

编辑

数据上下文:

namespace TestColumnHeaderCommandBinding
{

    public class Item
    {
        public int AData { get; set; }
        public string BData { get; set; }

    }


    public class MyCommand : ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            MessageBox.Show(parameter.ToString() + " clicked");
        }

        #endregion
    }

    public class DataContext
    {

        public DataContext()
        {
            MyCommand = new TestColumnHeaderCommandBinding.MyCommand();


            Items = new List<Item>(5);

            Items.Add(new Item { AData = 1, BData = "One" });
            Items.Add(new Item { AData = 2, BData = "Two" });
            Items.Add(new Item { AData = 3, BData = "Three" });
            Items.Add(new Item { AData = 4, BData = "Four" });
            Items.Add(new Item { AData = 5, BData = "Five" });
        }



        public MyCommand MyCommand { get; set; }


        public List<Item> Items { get; set; }

    }
}


推荐答案

如果没有明确指定标题的内容,它将只包含一个简单的字符串,它不会继承包含<$ c的 DataContext $ c> DataGrid 。

If you don't explicitly specify the content of the header, it will just contain a simple string, which will not inherit the DataContext of the containing DataGrid.

您应该在Visual Studio输出窗口中看到包含以下内容的警告:

You should see a warning in the Visual Studio output window containing something like this:

'MyCommand' property not found on 'object' ''String'

要解决这个问题,您可以将绑定目标定位到 DataGrid DataContext >:

To fix that, you can make the binding target the DataContext of the DataGrid:

<Setter Property="Command" 
        Value="{Binding DataContext.MyCommand,
          RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />

这篇关于DataGrid.ColumnHeaderStyle和命令绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆