首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataTemplate中的EventToCommand

DataTemplate中的EventToCommand
EN

Stack Overflow用户
提问于 2011-10-06 16:21:09
回答 1查看 3.1K关注 0票数 4

我使用MVVM-light-Toolkit中的EventToCommand类来处理WPF-DataGrid中的AutoGeneratingColumn-Event。它在我的主数据网格中工作得很好,但是我在RowDetailsTemplate中使用了另一个DataGrid,在这里我遇到了一个问题:在生成EventToCommand-Object之前触发AutoGeneratingColumn。这个问题有解决方案吗?这是我的Xaml代码的一部分:

代码语言:javascript
复制
<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>

调用代码隐藏文件中的事件处理程序WorkpieceListAutoGeneratingColumn,而从不调用my ViewModel中的命令。

安德烈亚斯

EN

回答 1

Stack Overflow用户

发布于 2013-02-05 19:35:07

你不必在后面使用邪恶的代码;-)你可以使用附加的行为来做到这一点……

代码语言:javascript
复制
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中使用它,如下所示。

代码语言:javascript
复制
<DataGrid ItemsSource="{Binding MyGridSource}"
          AttachedCommand:AutoGeneratingColumnEventToCommandBehaviour.Command="{Binding CreateColumnsCommand}">
</DataGrid>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7671783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档