MVVM - WPF DataGrid - AutoGeneratingColumn 事件 [英] MVVM - WPF DataGrid - AutoGeneratingColumn Event

查看:31
本文介绍了MVVM - WPF DataGrid - AutoGeneratingColumn 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在仔细研究 Laurent 的 优秀 工具包,但我有以下问题.

I'm currently taking a good look at the excellent toolkit from Laurent and I have the following question.

从 Blend 4 开始,我为 Loaded 事件添加了一个 EventTrigger,在我的 ViewModel 中,我有以下内容:

From Blend 4, I have added an EventTrigger for the Loaded event, in my ViewModel I have the following:

public RelayCommand rcAutoGeneratingColumn { get; private set; }

在构造函数中我有:

rcAutoGeneratingColumn = 
   new RelayCommand(o => DataGridAutoGeneratingColumn(o));

也在 ViewModel 中,我有我希望被 RelayCommand 调用的方法:

Also in the ViewModel, I have the method which I wish to be invoked by the RelayCommand:

    private void DataGridAutoGeneratingColumn(Object o)
    {
        DataGrid grid = (DataGrid)o;

        foreach (DataGridTextColumn col in grid.Columns)
        {
            if (col.Header.ToString().ToLower() == "id")
            {
                col.Visibility = System.Windows.Visibility.Hidden;
            }
        }
    }

我的 XAML 包含以下内容(对于 DataGrid):

My XAML contains the following (for the DataGrid):

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <GalaSoft_MvvmLight_Command:EventToCommand 
            Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
            CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

这里没有问题代码工作得很好,但显然用于隐藏某些列的事件应该是 AutoGeneratingColumn 事件而不是 Loaded.我已经习惯将 Loaded 事件作为一种解决方法.

There is NO PROBLEM here the code works just fine, but obviously the event used to hide certain columns should be the AutoGeneratingColumn event and not Loaded. I have used to Loaded event as a getaround.

我希望我可以中继控件提供的任何事件,以便在这种情况下,以下内容将起作用:

I was hoping that I could relay any event offered by the control so that, in this case, the following would work instead:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="AutoGeneratingColumn">
        <GalaSoft_MvvmLight_Command:EventToCommand 
            Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
            CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

我无法触发 AutoGeneratingColumn 事件,我希望我忽略了一些东西并感谢您提供的任何建议!

I am unable to get the AutoGeneratingColumn event to trigger, and I'm hoping that I've overlooked something and appreciate any advice given!

此行为与来自 DevExpress 的 GridControl 相同,因为 Loaded 事件被触发,而 ColumnsPopulated 事件(这相当于 AutoGeneratingColumn 事件)不是.

This behaviour is the same with the GridControl from DevExpress, in that the Loaded event is triggered whereas the ColumnsPopulated event (this being the equivalent of the AutoGeneratingColumn event) is not.

DevExpress 针对我的问题提供了以下信息:

DevExpress offered the following information with regard to my question:

我们已经回顾了这个问题,并得出了一个有趣的结论.看起来在处理 Interaction.Triggers 时没有构建可视化树"

如果这是真的,并且没有其他方法可以调用 ViewModel 中的事件,那么必须继续 - 通过反复试验 - 注意哪些 DataGrid 事件(其中有超过 100) 可以通过这种方式调用,哪些不能!

If this is true, and there is no other way in which to invoke the events within the ViewModel, then one would have to go ahead and - by using trial and error - note which of the DataGrid events (of which there are over 100) can be invoked in this way and which cannot!

人们认为,在应用 MVVM 模式时,代码隐藏中可用的每个事件也可以达到.

One would like to think that every event which is available in the code-behind, can also be reached when applying the MVVM pattern.

我已经搜索了一个答案,但我不能排除我忽略了一些东西,所以如果是这种情况,那么请接受我的道歉!

I have searched for an answer but I cannot rule out that I have overlooked something, so if this is to be the case, then please accept my apologies!

推荐答案

在使用 MVVM 开发项目的过程中,您将遇到必须在视图的代码隐藏中处理事件而 EventToCommand 只是简单的情况"工作.您尤其在 Silverlight 中发现了这一点,但我从您的问题中假设您正在使用 WPF.在视图的代码隐藏中做一些事件处理是可以的,只是不要在那里放置任何业务逻辑.你甚至可以将命令留在你的视图模型中,直接从你的事件处理程序中调用它.

During the course of developing a project with MVVM you're going to have circumstances where you must handle events in your view's code-behind and EventToCommand just plain doesn't work. You especially find this with Silverlight, but I assume from your question that you're using WPF. It's okay to do some event handling in your view's code-behind, just don't put any business logic there. You can even leave the command in your view model, just call it directly from your event handler.

((YourViewModel)this.DataContext).rcAutoGeneratingColumn.Execute(sender);

这篇关于MVVM - WPF DataGrid - AutoGeneratingColumn 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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