【问题标题】:WPF Datagrid's OnGeneratingColumn Not firing when using Event Triggers使用事件触发器时 WPF Datagrid 的 OnGeneratingColumn 未触发
【发布时间】:2018-10-15 18:08:46
【问题描述】:

我正在尝试在一个简单的级别上对此进行测试,其中我有以下 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 行中:&lt;local:TasksDatagridView x:Name="tview" /&gt; 它说它抛出了 system.exception,但代码仍然可以编译并运行良好。

  2. AutoGeneratingColumn 没有被触发。

真的,我想弄清楚 #2 以及为什么这个特定事件没有触发。现在我在执行方法中有一个简单的打印,当用简单的单击或加载数据网格的事件替换事件名称时,命令工作正常并且几乎任何其他事件都会被触发,这告诉我它不是我的视图模型或委托命令类。关于为什么自动生成列事件不适用于命令的任何想法?请注意,我已确保事件名称没有拼写错误。

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

【问题讨论】:

  • 你能分享GenColumns的代码吗?
  • @Sham 它是一个简单的打印 "System.Windows.MessageBox.Show("Event Fired"); 并且如前所述,它适用于大多数其他事件,包括 datagrid 的加载事件
  • @arias_JC 您检查控制台是否有任何绑定错误?
  • @DipenShah 是的,没有与相关控件相关联。另外,我可以使用 find 后面的代码调用 fire AutoGeneratingColumn,它只是没有任何意义,为什么它不能通过交互命令工作,并且仅适用于该事件
  • @arias_JC 你还能发布这些错误吗?

标签: c# wpf xaml mvvm


【解决方案1】:

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

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

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);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多