数据模板中的 EventToCommand

发布于 2024-12-08 08:23:21 字数 2061 浏览 0 评论 0原文

我使用 MVVM-light-Toolkit 中的 EventToCommand 类来处理 WPF-DataGrid 中的 AutoGenerateColumn-Event。它在我的 Main-DataGrid 中工作正常,但我在 RowDetailsTemplate 中使用另一个 DataGrid,在这里我遇到了问题: AutoGenerateColumn 在生成 EventToCommand-Object 之前触发。这个问题有解决办法吗? 这是我的 Xaml 代码的一部分:

<DataGrid DockPanel.Dock="Top" AutoGenerateColumns="True" Name="table" VerticalAlignment="Top" ItemsSource="{Binding PartBatchList}" IsReadOnly="True">
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="AutoGeneratingColumn">
                <hgc:EventToCommand Command="{Binding AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Margin="30,0,30,30" Orientation="Vertical">
                <Border CornerRadius="4" Padding="5" Background="White">
                    <DataGrid ItemsSource="{Binding Workpieces}"  
                                    CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"
                                    AutoGenerateColumns="True" AutoGeneratingColumn="WorkpieceListAutoGeneratingColumn">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="AutoGeneratingColumn">
                                <hgc:EventToCommand Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid},AncestorLevel=2}, Path=DataContext.AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </DataGrid>
                </Border>
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

代码隐藏文件中的事件处理程序 WorkpieceListAutoGenerateColumn 被调用,我的 ViewModel 中的命令从未被调用。

安德烈亚斯

I'm using the EventToCommand Class from the MVVM-light-Toolkit to handle the AutoGeneratingColumn-Event in the WPF-DataGrid. It works fine in my Main-DataGrid, but I use another DataGrid in the RowDetailsTemplate and here I got a problem:
The AutoGeneratingColumn fires before the EventToCommand-Object was generated. Is there a solution for this problem?
Here is a piece of my Xaml-Code:

<DataGrid DockPanel.Dock="Top" AutoGenerateColumns="True" Name="table" VerticalAlignment="Top" ItemsSource="{Binding PartBatchList}" IsReadOnly="True">
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="AutoGeneratingColumn">
                <hgc:EventToCommand Command="{Binding AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Margin="30,0,30,30" Orientation="Vertical">
                <Border CornerRadius="4" Padding="5" Background="White">
                    <DataGrid ItemsSource="{Binding Workpieces}"  
                                    CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"
                                    AutoGenerateColumns="True" AutoGeneratingColumn="WorkpieceListAutoGeneratingColumn">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="AutoGeneratingColumn">
                                <hgc:EventToCommand Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid},AncestorLevel=2}, Path=DataContext.AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </DataGrid>
                </Border>
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

The Event-Handler WorkpieceListAutoGeneratingColumn in the Code-Behind File is called, the Command in my ViewModel is never called.

Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-12-15 08:23:21

原因应该是你不能在同一个对象/事件组合上有一个事件处理程序和一个事件命令。从 DataGrid 中删除 AutoGenerateColumn="WorkpieceListAutoGenerateColumn" 并调用该命令。

我自己也遇到过这个问题:-)

编辑

如果您需要后面代码中的事件处理程序,请删除 EventToCommand 并调用命令在你的代码后面,例如

public void WorkpieceListAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs args) {
    var vm = ((YourViewModel) this.DataContext);
    if (vm.AutoGeneratingColumnCommand.CanExecute(eventArgs)) 
        vm.AutoGeneratingColumnCommand.Execute(eventArgs);
}

但是,我认为第一个选项是更好的。

编辑 2

好的,环顾四周,似乎 仅在对象已渲染且用户交互发生后才起作用(因此得名?)。嗯,这意味着只有一些事件(在对象构造期间调用的事件)无法由 EventToCommand 机制处理。在这些情况下,可以使用隐藏代码从那里调用您的命令,请参阅我的第一次编辑。

The reason should be that you can't have an event andler and an event to command on the same object/event combination. Remove the AutoGeneratingColumn="WorkpieceListAutoGeneratingColumn" from your DataGrid an the command should be called.

Had the problem once myself :-)

Edit

If you need the eventhandler in the code behind, remove the EventToCommand and call the command in your code behind, e.g.

public void WorkpieceListAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs args) {
    var vm = ((YourViewModel) this.DataContext);
    if (vm.AutoGeneratingColumnCommand.CanExecute(eventArgs)) 
        vm.AutoGeneratingColumnCommand.Execute(eventArgs);
}

But, I think that the first option is the better one.

Edit 2

OK, had some look around and it seems that <i:Interaction.Triggers/> only works after the object is already rendered and user interaction takes place (hence the name?). Well, this means that there are simply just some events - the ones that are called during the construction of the object - that cannot be handled by the EventToCommand mechanism. In these cases it is OK to use code behind to call your command from there, see my first edit.

花心好男孩 2024-12-15 08:23:21

您不必在后面使用邪恶的代码;-) 您可以使用附加行为来执行此操作...

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

然后在 XAML 中使用它,如下所示...

<DataGrid ItemsSource="{Binding MyGridSource}"
          AttachedCommand:AutoGeneratingColumnEventToCommandBehaviour.Command="{Binding CreateColumnsCommand}">
</DataGrid>

You don't have to use evil code behind ;-) You can do this using an attached behaviour...

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

Then use it in XAML like this...

<DataGrid ItemsSource="{Binding MyGridSource}"
          AttachedCommand:AutoGeneratingColumnEventToCommandBehaviour.Command="{Binding CreateColumnsCommand}">
</DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文