开发者

EventToCommand in DataTemplate

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


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜