How do you reference the appropriate object from a DataTemplate in XAML?

I want to use the CommandParameter attribute in the context menu associated with the DataTemplate. The commandParameter parameter must contain a reference to the object that initiated the data template, as shown in the code example below. I tried to use "{Binding Path = this}", but this does not work because "this" is not a property. The command fires, but I cannot get the correct parameter. Does anyone have an idea on how to do this?

Note. I deleted Command = "{Binding DeleteSelectedMeetingCommand}", replacing it with a reference to the view locator and initiating the command.

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

Thank,

+3
2

. {Binding} CommandParameter, , 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>
+3

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
}

, .

0

Source: https://habr.com/ru/post/1754783/


All Articles