簡體   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