我不明白为什么CommandParameter在Datagrid中绑定ElementName = null Cat.Wpf
<Window x:Class="MyCat.Cat"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyCat"
xmlns:vm ="clr-namespace:MyCat.ViewModel"
mc:Ignorable="d" WindowStartupLocation="CenterScreen"
Title="MyCat •" Height="650" Width="800">
<Window.DataContext>
<vm:MyFriends/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Grid.Row="0" Grid.Column="0"
Header="Show Name" x:Name="mimi"
Command="{Binding GetNameCat}"
CommandParameter="{Binding ElementName=mimi}"/>
</Menu>
<DataGrid CanUserAddRows="False" CanUserDeleteRows="False"
Grid.Row="1" Grid.Column="0"
SelectionUnit="FullRow">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="List cats" x:Name="cats"
Command="{Binding GetCats}"
CommandParameter="{Binding ElementName=cats}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
CommandParameter="{Binding ElementName=mimi}"
有效,但是CommandParameter="{Binding ElementName=cats}
不起作用。它总是返回null。我找出解决方案
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
。
有些身体,请告诉我为什么?