C#wpf数据绑定命令在contextmenu中不起作用
我正在学习wpf和mvvm,我决定为自己创建一个Soundboard来练习,到目前为止它已经很好。 现在我已经创建了一个数据模板,其中程序在指定目录中找到的每个文件都会创建一个带有文件名称的按钮,然后我可以点击它来播放。到现在为止还挺好。 但是我现在试图创建一个ContextMenu,以便当我想从列表中删除一个文件时,我可以右键单击并选择删除,但即使我具有与常规按钮完全相同的命令结构,该命令也不起作用。C#wpf数据绑定命令在contextmenu中不起作用
我真的很困惑整个RelativeSource的东西,并已经很高兴我的常规'玩'命令在按钮工作。
如果有人能指出我的方向会很棒。我真的可以对我的具体问题进行解释,因为总是似乎以某种方式帮助我更多的是一个通用示例。我试图阅读所有相关的问题,但似乎没有从那里弄清楚。
我的ItemsControl:
<ItemsControl x:Name="MySounds" ItemsSource="{Binding Sounds}">
ItemTemplate:它
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Style="{StaticResource mainButton}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}"
CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
Tag="{Binding Path=Name}">
<TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" />
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Path=Name}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.RemoveSound}"
CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}">
<MenuItem.Icon>
<Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
我有一个通用的RelayCommand在我的视图模型,所有的作品,真正的问题是刚刚与结合。
如果您在Button
的Tag
属性绑定到ItemsControl
,你可以使用的的PlacementTarget
属性绑定到命令ContextMenu
:
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Style="{StaticResource mainButton}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}"
CommandParameter="{Binding Path=Name}"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}">
<TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" />
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Path=Name}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.RemoveSound}"
CommandParameter="{Binding Path=Name}">
<MenuItem.Icon>
<Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
这似乎已经做到了。 –
你能否详细说明为什么做这个工作?是否因为Tag现在被设置为RelativeSource ItemsControl,然后PlacementTarget被设置为Tag并且又被设置为DataContext?就像我之前说过的,我对RelativeSource的理解是非常光明的。 –
ContextMenu驻留在它自己的可视化树中,因此它没有任何祖先。虽然按钮。 – mm8
您可以尝试通过它来取代你的命令字符串中的菜单项:
Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.RemoveSound}"
你可以尝试使用'ElementName'而不是'RelativeSource'吗?为您的ItemsControl设置一个名称(如x:Name =“myIC”),然后更新绑定。 – Atlasmaybe
我试过了,但无论我尝试过什么,它都不起作用。在我的MenuItem头文件中,我使用'{binding Path = Name}'获得了与我的按钮相同的值,但是同样的命令似乎不起作用。 –