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>

谢谢,

4

2 回答 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>
于 2010-07-15T11:25:10.587 回答
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
}

希望有帮助。

于 2010-07-15T10:53:41.690 回答