TemplateBinding作为InvokeCommandAction CommandParameter不起作用?

问题描述

我正在尝试为要在窗口中多次使用的控件编写可重用的模板。在其中,我有一些带有CommandParameter的命令。 CommandParameter是我要通过此模板的“ Tag”属性传递的对象。

对于“ MenuItem” CommandParameter和StackPanel“ DataContext”,这很好。但是对于通过InvokeCommandAction进行的“ MouseDown”事件(或“ PreviewMouseDown”,我都尝试了),我只获得了null作为参数。它会触发命令并调用正确的方法,但参数始终为null。

这里是我的Xaml(简称):

<Window.Resources>
        <ControlTemplate x:Key="FavControl" targettype="ContentControl">
            <StackPanel>
                <StackPanel.ContextMenu>
                    <ContextMenu IsEnabled="{Binding FavContextMenuEnabled}">
                        <MenuItem Header="Bearbeiten"
                                  Command="{Binding FavContextMenuEditCmd}"
                                  CommandParameter="{TemplateBinding Tag}" />
                        <MenuItem Header="Löschen"
                                  Command="{Binding FavContextMenuDeleteCmd}"
                                  CommandParameter="{TemplateBinding Tag}" />
                        <Separator IsEnabled="{Binding UserisDev}" />
                        <MenuItem Header="Entwicklung"
                                  Command="{Binding FavContextMenuStartDevCmd}"
                                  CommandParameter="{TemplateBinding Tag}"
                                  IsEnabled="{Binding UserisDev}" />
                        <MenuItem Header="Starte Objekt"
                                  Command="{Binding FavContextMenuRunObjectCmd}"
                                  CommandParameter="{TemplateBinding Tag}"
                                  IsEnabled="{Binding UserisDev}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDown">
                        <i:InvokeCommandAction Command="{Binding FavoriteClickCmd}" CommandParameter="{TemplateBinding Tag}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <StackPanel Orientation="Vertical" DataContext="{TemplateBinding Tag}">
                    <Image Source="{Binding FavImage,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" 
                           HorizontalAlignment="Center" VerticalAlignment="Center" Height="70" Width="70" />
                    <TextBlock Text="{Binding Description}" HorizontalAlignment="Center" />
                </StackPanel>
            </StackPanel>
        </ControlTemplate>
    </Window.Resources>

<!-- ... -->

<ContentControl Grid.Row="0" Grid.Column="0" Template="{StaticResource FavControl}" Tag="{Binding FavoriteArray[0]}" />
<ContentControl Grid.Row="0" Grid.Column="1" Template="{StaticResource FavControl}" Tag="{Binding FavoriteArray[1]}" />
<ContentControl Grid.Row="0" Grid.Column="2" Template="{StaticResource FavControl}" Tag="{Binding FavoriteArray[2]}" />

<!-- ... -->

我需要方法背后的代码中的此参数才能知道要使用哪个对象。那么,如何传递该对象?为什么TemplateBinding不起作用?有人可以告诉我,我在做什么错吗?

P.S .:很抱歉出现错误,英语不是我的母语:)

解决方法

使用TemplateBinding绑定到RelativeSource来代替TemplatedParent

<b:InvokeCommandAction Command="{Binding FavoriteClickCmd}"
                       CommandParameter="{Binding Tag,RelativeSource={RelativeSource TemplatedParent}}" />