如何在XAML中引用DataTemplate中的匹配对象?

3
我想在与DataTemplate相关联的上下文菜单中使用CommandParameter属性。CommandParameter应包含对触发数据模板的对象的引用,如下面的代码示例所示。我尝试使用"{Binding Path=this}",但它不起作用,因为"this"不是一个属性。命令会触发,但我无法获得正确的参数。有没有人有关于如何做到这一点的想法?
注意:我通过将其替换为对视图定位器的引用而删除了Command="{Binding DeleteSelectedMeetingCommand}",并且命令正在触发。
       <DataTemplate DataType="{x:Type Models:MeetingDbEntry}">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"  Text="{Binding Path=HostTeam}"/>
            <TextBlock Grid.Column="1" Text="{Binding Path=GuestTeam}"/>
            <TextBlock Grid.Column="2" Text="{Binding Path=Result}"/>
            <Grid.ContextMenu>
                <ContextMenu Name="MeetingMenu">
                    <MenuItem Header="Delete"  
                              Command="{Binding 
                                            Source={StaticResource Locator}, 
                                            Path=Main.DeleteSelectedMeetingCommand}"
                              CommandParameter="{Binding Path=this}"/>
                </ContextMenu>
            </Grid.ContextMenu>
        </Grid>
    </DataTemplate>

Thanks,

2个回答

3
以下代码可以正常工作。您只需要在CommandParameter属性中键入{Binding},即可引用触发DataTemplate的属性。
 <DataTemplate DataType="{x:Type Models:MeetingDbEntry}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"  Text="{Binding Path=HostTeam}"/>
            <TextBlock Grid.Column="1" Text="{Binding Path=GuestTeam}"/>
            <TextBlock Grid.Column="2" Text="{Binding Path=Result}"/>
            <Grid.ContextMenu>
                <ContextMenu Name="MeetingMenu">
                    <MenuItem Header="Delete"  
                              Command="{Binding 
                                      Source={StaticResource Locator}, 
                                      Path=Main.DeleteSelectedMeetingCommand}"
                              CommandParameter="{Binding}"
                              />

                </ContextMenu>
            </Grid.ContextMenu>
        </Grid>
    </DataTemplate>

0

我会在删除对象上公开DeleteSelectedMeetingCommand,并将上下文菜单条目绑定到它。然后,添加一个成员变量来保存要删除的对象,并在持有该命令的要删除的对象中使用this进行初始化。

例如:

public class DeletableObject
{
    public ICommand DeleteCommand { get; }

    public DeleteableObject()
    {
        DeleteCommand = new DeleteCommand(this);
    }
}

public class DeleteCommand : ICommand
{
    private DeletableObject _DeletableObject;

    public DeleteCommand(DeletableObject deletableObject)
    {
        _DeletableObject = deletableObject;
    }

    // skipped the implementation of ICommand but it deletes _DeletableObject
}

希望这有所帮助。

谢谢你的回答。事实上,我正在使用类似的方法,将选定的项目绑定到属性,并使用没有参数的命令。然后,该命令使用所选属性来删除对象。 但是,我正在尝试探索MVVM Light工具包中RelayCommand的模板化版本,并且我想直接通过XAML绑定到RelayCommand,而不必为绑定编写特定的代码(我已经成功通过click属性进行了绑定)。 - Amine Kerkeni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接