简体   繁体   English

WPF Datagrid的OnGeneratingColumn在使用事件触发器时不触发

[英]WPF Datagrid's OnGeneratingColumn Not firing when using Event Triggers

I am trying to test this at a simple level where I have the following TasksDatagridView.xaml: 我正在尝试在具有以下TasksDatagridView.xaml的简单级别上对此进行测试:

<UserControl x:Class="Example.Views.TasksDatagridView" ...>
    <UserControl.Resources>
        <local:CompleteConverter x:Key="completeConverter" />
        <local:Tasks x:Key="tasks" />
        <CollectionViewSource x:Key="cvsTasks" Source="{Binding Path=tasks}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="ProjectName"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>
    <Grid>
        <DataGrid x:Name="myDG" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource cvsTasks}}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="AutoGeneratingColumn">
                    <i:InvokeCommandAction Command="{Binding GenColumns}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>
    </Grid>
</UserControl>

In my TasksDatagridView.xaml.cs I tried both setting the datacontext first this.DataContext = new ViewModels.TaskDgVm() and then InitializeComponent() and vice versa. 在我的TasksDatagridView.xaml.cs中,我尝试同时设置数据this.DataContext = new ViewModels.TaskDgVm() ,然后设置InitializeComponent() ,反之亦然。

In my main window MainWindow.xaml I reference the control like such: 在主窗口MainWindow.xaml中,我引用了如下控件:

<Window x:Name="MainWindow" x:Class="Example.Views.MyMainWindowView" ...>
  <Grid>
    <local:TasksDatagridView x:Name="tview" />
  </Grid>
</Window>

This is a derived example that shows the point so please excuse mispelling. 这是一个派生的示例,它说明了这一点,因此请原谅拼写错误。 So I am having two issues: 所以我有两个问题:

  1. In the MainWindow.xaml line where i reference the control: <local:TasksDatagridView x:Name="tview" /> it says it threw a system.exception, yet the code still compiles and runs fine. 在我引用控件的MainWindow.xaml行中: <local:TasksDatagridView x:Name="tview" />它说它抛出了system.exception,但代码仍然可以编译并运行良好。

  2. AutoGeneratingColumn is not being fired. AutoGeneratingColumn没有被触发。

Really I am trying to figure out #2 and why this specific event is not firing. 确实,我正在尝试找出#2以及为何此特定事件未触发。 Right now I have a simple print in the execute method and when replacing the event name with a simple click or loaded event for the datagrid the command works fine and just about any other event gets fired, which tells me its not something in my viewmodel or delegate command class. 现在,我在execute方法中具有简单的打印内容,当用简单的click或loaddata事件替换事件名称时,该命令可以正常工作,并且几乎触发了任何其他事件,这告诉我在我的viewmodel中没有任何内容委托命令类。 Any thoughts on why the autogenerate column event is not working with command? 关于为什么自动生成列事件不能与命令一起使用的任何想法? Note I have made sure the event name is not misspelled. 注意我确保事件名称没有拼写错误。

Edit: After posting question I found a related question here: MVVM - WPF DataGrid - AutoGeneratingColumn Event However they use mvvm-light toolkit where I am using the expression blend interactivity library. 编辑:发布问题后,我在这里找到了一个相关问题: MVVM-WPF DataGrid-AutoGeneratingColumn事件但是,他们使用mvvm-light工具箱,而我正在使用表达式混合交互库。 Although the same answer may apply to both questions, they are indeed two separate toolkits. 尽管相同的答案可能适用于两个问题,但它们确实是两个单独的工具箱。

So based on this thread MVVM - WPF DataGrid - AutoGeneratingColumn Event I believe the visual tree is not getting constructed during some of these events. 因此,基于该线程, MVVM-WPF DataGrid-AutoGeneratingColumn事件我相信在其中一些事件期间不会构建可视树。

But there is an alternative provided that solves the problem while avoiding code behind: 但是提供了一种替代方法,可以解决问题,同时避免后面的代码:

public class AutoGeneratingColumnEventToCommandBehaviour
{
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached(
            "Command", 
            typeof(ICommand), 
            typeof(AutoGeneratingColumnEventToCommandBehaviour),
            new PropertyMetadata(
                null,
                CommandPropertyChanged));

    public static void SetCommand(DependencyObject o, ICommand value)
    {
        o.SetValue(CommandProperty, value);
    }

    public static ICommand GetCommand(DependencyObject o)
    {
        return o.GetValue(CommandProperty) as ICommand;
    }

    private static void CommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dataGrid = d as DataGrid;
        if (dataGrid != null)
        {
            if (e.OldValue != null)
            {
                dataGrid.AutoGeneratingColumn -= OnAutoGeneratingColumn;
            }
            if (e.NewValue != null)
            {
                dataGrid.AutoGeneratingColumn += OnAutoGeneratingColumn;
            }
        }
    }

    private static void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var dependencyObject = sender as DependencyObject;
        if (dependencyObject != null)
        {
            var command = dependencyObject.GetValue(CommandProperty) as ICommand;
            if (command != null && command.CanExecute(e))
            {
                command.Execute(e);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM