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

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

问题描述

我目前正在研究Laurent的 Excellent 工具包,并且我有以下问题.

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事件作为解决方法.

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天全站免登陆