繁体   English   中英

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

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

我正在尝试在具有以下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>

在我的TasksDatagridView.xaml.cs中,我尝试同时设置数据this.DataContext = new ViewModels.TaskDgVm() ,然后设置InitializeComponent() ,反之亦然。

在主窗口MainWindow.xaml中,我引用了如下控件:

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

这是一个派生的示例,它说明了这一点,因此请原谅拼写错误。 所以我有两个问题:

  1. 在我引用控件的MainWindow.xaml行中: <local:TasksDatagridView x:Name="tview" />它说它抛出了system.exception,但代码仍然可以编译并运行良好。

  2. AutoGeneratingColumn没有被触发。

确实,我正在尝试找出#2以及为何此特定事件未触发。 现在,我在execute方法中具有简单的打印内容,当用简单的click或loaddata事件替换事件名称时,该命令可以正常工作,并且几乎触发了任何其他事件,这告诉我在我的viewmodel中没有任何内容委托命令类。 关于为什么自动生成列事件不能与命令一起使用的任何想法? 注意我确保事件名称没有拼写错误。

编辑:发布问题后,我在这里找到了一个相关问题: MVVM-WPF DataGrid-AutoGeneratingColumn事件但是,他们使用mvvm-light工具箱,而我正在使用表达式混合交互库。 尽管相同的答案可能适用于两个问题,但它们确实是两个单独的工具箱。

因此,基于该线程, MVVM-WPF DataGrid-AutoGeneratingColumn事件我相信在其中一些事件期间不会构建可视树。

但是提供了一种替代方法,可以解决问题,同时避免后面的代码:

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