不带标签的 DataTemplate 中 MenuItem 的绑定命令

发布于 2024-10-14 04:46:03 字数 1164 浏览 6 评论 0原文

我想将 ViewModel 中的命令绑定到 DataTemplate 中的 menuItem。我可以使用标签来做到这一点。有没有任何方法可以完成相同的任务但不使用标签。

<Window.Resources>
    <DataTemplate x:Key="StudentListBoxItemTemplate">
        <StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
            <TextBlock Text="{Binding Name}"/>
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Trigger" Command="{Binding PlacementTarget.Tag.TriggerCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                </ContextMenu>
            </StackPanel.ContextMenu>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ListBox 
        ItemsSource="{Binding StudentList}" 
        ItemTemplate="{StaticResource StudentListBoxItemTemplate}">
    </ListBox>
</StackPanel>

我的视图模型

public class MainViewModel {
    public ICommand TriggerCommand { ... }
    public ObservableList<Student> StudentList { ... }
}

I want to bind a command in my ViewModel to a menuItem which is in DataTemplate. I can do that with using Tag. Is there any method which can do the same task but without using tag.

<Window.Resources>
    <DataTemplate x:Key="StudentListBoxItemTemplate">
        <StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
            <TextBlock Text="{Binding Name}"/>
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Trigger" Command="{Binding PlacementTarget.Tag.TriggerCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                </ContextMenu>
            </StackPanel.ContextMenu>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ListBox 
        ItemsSource="{Binding StudentList}" 
        ItemTemplate="{StaticResource StudentListBoxItemTemplate}">
    </ListBox>
</StackPanel>

My ViewModel

public class MainViewModel {
    public ICommand TriggerCommand { ... }
    public ObservableList<Student> StudentList { ... }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-10-21 04:46:03

根据您当前的设计,您需要从 ContextMenu 通过 StackPanel 并返回到包含 ListBoxDataContext代码>.造成这种尴尬的原因是 StackPanelDataContext 已经缩小到特定的学生。

至少有两种方法可以让这变得更容易:

  • Student 中提供 TriggerCommand 属性,以便该命令就在您需要的地方
  • 提供 Parent Student 中的 > 属性来逃避缩小的范围

With your current design, you need to get from the ContextMenu through the StackPanel and back to the DataContext of the containing ListBox. What makes this awkward is that the DataContext of the StackPanel is already narrowed down to a particular student.

There are at least two ways to make this easier:

  • Provide a TriggerCommand property in Student so the command is right there where you need it
  • Provide a Parent property in the Student to escape the narrowed scope
半暖夏伤 2024-10-21 04:46:03

您可以尝试将点击事件添加到menuItem,如下

<Menu Style="{StaticResource bellRingersFontStyle}" Height="23" Name="menu1" Width="Auto" DockPanel.Dock="Top" VerticalAlignment="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New Member" Name="newMember" Click="newMember_Click" >
                    <MenuItem.Icon>
                        <Image Source="Face.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Save Member Details" Name="saveMember" IsEnabled="False" Click="saveMember_Click">
                    <MenuItem.Icon>
                        <Image Source="Note.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="E_xit" Name="exit" Click="exit_Click"  />
            </MenuItem>
            <MenuItem Header="_Help">
                <MenuItem Header="_About Middleshire Bell Ringers" Name="about" Click="about_Click" >
                    <MenuItem.Icon>
                        <Image Source="Ring.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>

尝试将命令绑定到Click。我的 VS 已关闭,因此目前无法检查。

You can try to add click event to the menuItem like following

<Menu Style="{StaticResource bellRingersFontStyle}" Height="23" Name="menu1" Width="Auto" DockPanel.Dock="Top" VerticalAlignment="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New Member" Name="newMember" Click="newMember_Click" >
                    <MenuItem.Icon>
                        <Image Source="Face.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Save Member Details" Name="saveMember" IsEnabled="False" Click="saveMember_Click">
                    <MenuItem.Icon>
                        <Image Source="Note.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="E_xit" Name="exit" Click="exit_Click"  />
            </MenuItem>
            <MenuItem Header="_Help">
                <MenuItem Header="_About Middleshire Bell Ringers" Name="about" Click="about_Click" >
                    <MenuItem.Icon>
                        <Image Source="Ring.bmp" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>

Try binding the command to Click. My VS is down so can't check at this moment.

紫轩蝶泪 2024-10-21 04:46:03

一种方法是获取视图模型中定义的上下文菜单集合演示,其中将包含标题字符串和命令操作(可能带有谓词)。

视图模型创建上下文菜单项的可观察集合,视图将其绑定到上下文菜单项源并将显示成员路径设置为标题字符串。

One way would be to get the context menu collection presentation defined in your viewmodel, which will contain the header string and command action (maybe with predicate).

The viewmodel creates an observable collection of the contextmenu items and the view binds that to ContextMenu itemssource and sets the displaymember path to header string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文