L-F*_*our 1 .net tags binding contextmenu datatemplate
我有:
<ListBox>
<ListBox.Resources>
<DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
<DockPanel>
<Button Content="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Command="{Binding PlacementTarget.Tag.DataContext.RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我想要实现的是将上下文菜单的menuitem中的命令绑定到在视图模型中定义的ICommand,该视图模型是列表框的datacontext,而命令参数应该是StyleViewModel,但是我尝试的不是'工作.谁能指出我正确的方向?
找到了!
<ListBox ItemsSource="{Binding ActiveCustomer.Member1FavoriteStyles}" ItemsPanel="{StaticResource ListBoxStyleItemsPanelAsVerticalStackPanel}" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}" Background="Transparent" BorderThickness="0">
<ListBox.Resources>
<DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
<DockPanel>
<Button Content="{Binding Name}" Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove" Command="{Binding PlacementTarget.Tag.RemoveMember1FavoriteStyleCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandParameter="{Binding}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
Run Code Online (Sandbox Code Playgroud)