繁体   English   中英

C#WPF数据绑定命令在上下文菜单中不起作用

[英]C# wpf Databinding command not working in contextmenu

我正在学习wpf和mvvm,因此我决定为自己创建一个Soundboard进行练习,到目前为止一切进展顺利。 现在,我制作了一个datatemplate,在该程序中,该程序在指定目录中找到的每个文件都将创建一个带有文件名的按钮,然后单击该按钮即可播放。 到现在为止还挺好。 但是,我现在尝试制作一个ContextMenu,以便当我想要从列表中删除文件时,可以右键单击并选择remove,但是即使我的常规按钮具有完全相同的命令结构,该命令也无法使用。

我真的对整个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,并且一切正常,问题确实出在绑定上。

您可以尝试通过以下方式替换MenuItem中的命令字符串:

Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.RemoveSound}"

如果将ButtonTag属性绑定到ItemsControl ,则可以使用ContextMenuPlacementTarget属性绑定到命令:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM