WPF绑定ContextMenu MenuItem的ItemsSource [英] WPF Binding ContextMenu MenuItem's ItemsSource

查看:357
本文介绍了WPF绑定ContextMenu MenuItem的ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个MenuItem的ItemsSource绑定到位于ViewModel中的 ReadOnlyCollection< string> 。我已经看到ContextMenu不在主Visual Tree下,所以我无法直接绑定,但我尝试的任何方法都不起作用。我有代码片段,请让我知道我做错了什么。

I'm trying to bind a single MenuItem's ItemsSource to a ReadOnlyCollection<string>located in the ViewModel. I've read that the ContextMenu is not under the main Visual tree so i can't bind it directly, but any method i try doesn't work. I have the code snippet please let me know what am i doing wrong.

<Window>
…
<DockPanel>
  <!-- Task bar Icon -->
  <tb:TaskbarIcon x:Name="AppNotifyIcon"
       DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
       ToolTipText="{Binding Source={StaticResource LocalizedStrings}, Path=Strings.MainTitle}">
      <tb:TaskbarIcon.ContextMenu>
          <ContextMenu DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                <MenuItem Header="{Binding Source={StaticResource LocalizedStrings}, Path=Strings.NotifyIconOpen}" Click="MenuItem_Open_Click"/>
                <MenuItem Header="Technologies" ItemsSource="{Binding to the ReadOnlyCollection of string in ViewModel}">
                   <MenuItem.ItemContainerStyle>
                       <Style>
                          <Setter Property="MenuItem.Command" Value="{Binding <!--Command in ViewModel-->, RelativeSource={RelativeSource AncestorType=Window}}"/>
                          <Setter Property="MenuItem.CommandParameter" Value="{Binding}"/> <!—Binding to the menuItem Header item -->
                       </Style>
                   </MenuItem.ItemContainerStyle>
               </MenuItem>
               <MenuItem Header="{Binding Source={StaticResource LocalizedStrings}, Path=Strings.NotifyIconExit}" Click="MenuItem_Exit_Click"/>
          </ContextMenu>
      </tb:TaskbarIcon.ContextMenu>
   </tb:TaskbarIcon>
…
</DockPanel>

我试图绑定第二个MenuItem的ItemsSource,而在它的ItemContainerStyle中,我想绑定命令和commandParameter。
**更新:**我正在使用hardcodet的TaskbarIcon为wpf,如果重要。

I am trying to bind the second MenuItem's ItemsSource and inside it's ItemContainerStyle i want to bind the command and the commandParameter. **Update: ** i'm using hardcodet's TaskbarIcon for wpf, if it matters.

谢谢

推荐答案

Ilan在使用snoop实用程序的评论中的建议。
我看到在视觉树中,ContextMenu没有它的PlacementTarget指向它的父,TaskbarIcon(Weird ..),但它有一个附件属性从TaskbarIcon称为TaskbarIcon.ParentTaskbarIcon,所以我将ContextMenu的DataContext绑定到TaskbarIcon.ParentTaskbarIcon.Tag,并将其全部修复。

Ok, I have found the problem thanks to Ilan's suggestion in the comments of using snoop utility. I saw that in the visual tree, the ContextMenu didn't have its PlacementTarget to point to its parent, the TaskbarIcon (Weird..), but it had an Attached Property called TaskbarIcon.ParentTaskbarIcon from the TaskbarIcon, so i binded the ContextMenu's DataContext to the TaskbarIcon.ParentTaskbarIcon.Tag and that fixed it all.

<Window>
...
<DockPanel>
            <!-- Task bar Icon -->
            <tb:TaskbarIcon x:Name="AppNotifyIcon"
                        IconSource="pack://application:,,,/Icons/HwServerIcon.ico"
                        Tag="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        ToolTipText="{Binding Tag, RelativeSource={RelativeSource Self}}"><!--{Binding Source={StaticResource LocalizedStrings}, Path=Strings.MainTitle}-->
                <tb:TaskbarIcon.ContextMenu>
                    <ContextMenu DataContext="{Binding Path=(tb:TaskbarIcon.ParentTaskbarIcon).Tag, RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="{Binding Source={StaticResource LocalizedStrings}, Path=Strings.NotifyIconOpen}" Click="MenuItem_Open_Click"/>
                        <MenuItem Header="Technologies" ItemsSource="{Binding TechnologiesNames}">
                            <MenuItem.ItemContainerStyle>
                                <Style>
                                    <Setter Property="MenuItem.Command" Value="{Binding DataContext.OpenTechnology, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
                                    <Setter Property="MenuItem.CommandParameter" Value="{Binding}"/>
                                </Style>
                            </MenuItem.ItemContainerStyle>
                        </MenuItem>
                        <MenuItem Header="{Binding Source={StaticResource LocalizedStrings}, Path=Strings.NotifyIconExit}" Click="MenuItem_Exit_Click"/>
                    </ContextMenu>
                </tb:TaskbarIcon.ContextMenu>
            </tb:TaskbarIcon>

所以,TaskbarIcon的标签指向Window的DataContext,ContextMenu的DataContext指向Taskbar附带的属性ParentTaskbarIcon .Tag,从现在开始,每个绑定都像在视觉树中的窗口下执行。

So, the TaskbarIcon's Tag is pointing the Window's DataContext and the ContextMenu's DataContext is pointing the Taskbar's attached property ParentTaskbarIcon.Tag and from now every binding is performed like it was under the window in the visual tree.

这篇关于WPF绑定ContextMenu MenuItem的ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆