首页 新闻 会员 周边 捐助

如何绑定treeView 中右键菜单 MenuItem 的Command

0
悬赏园豆:10 [待解决问题]

有个关于WPF 中TreeView ContextMenu MenuItem 右键菜单,绑定Command的问题,请教大家一下代码如下

 1 <Controls:EverTreeView HorizontalAlignment="Left" Height="364" Margin="26,52,0,0" Name="ResourecesTreeView"  EverSelectedItem="{Binding SelectedTreeViewItem}"   ItemsSource="{Binding ResourcesSettingModel.TvResourceGroup,Mode=TwoWay}" VerticalAlignment="Top" Width="256"   >
 2             <Controls:EverTreeView.Resources>
 3 
 4                 
 5 
 6                 <ContextMenu x:Key="treeViewOperation">
 7                     <MenuItem Header="添加"  Command="{Binding DataContext.MenuAddCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>                              
 8                     <MenuItem Header="修改"  />
 9 
10 
11                     <MenuItem Header="删除" />
12                        
13 
14                 </ContextMenu>
15 
16                 </Controls:EverTreeView.Resources>
17 
18             <Controls:EverTreeView.ItemContainerStyle>
19                 <!-- 
20         This Style binds a TreeViewItem to a PersonViewModel. 
21         -->
22                 <Style TargetType="{x:Type TreeViewItem}"   >
23                     <Setter Property="IsExpanded" Value="{Binding ResourcesSettingModel.IsResourceTreeExpanded, Mode=TwoWay}" />
24                     <Setter Property="IsSelected" Value="{Binding ResourcesSettingModel.IsResourceTreeSelected, Mode=TwoWay}" />
25                    
26                     <Setter Property="FontWeight" Value="Normal"  />
27                  
28                     <Style.Triggers>
29                         <Trigger Property="IsSelected" Value="True">
30                             <Setter Property="FontWeight" Value="Bold" />
31                         </Trigger>
32                     </Style.Triggers>
33                     <!-- We have to select the item which is right-clicked on -->
34 
35                    
36 
37                 </Style>
38                 </Controls:EverTreeView.ItemContainerStyle>
39 
40                 <Controls:EverTreeView.ItemTemplate>
41                 <HierarchicalDataTemplate ItemsSource="{Binding ChildTreeViewItems}"  >
42                     <TextBlock Text="{Binding NodeName}"  ContextMenu="{StaticResource treeViewOperation}" />
43                 </HierarchicalDataTemplate>
44 
45                 </Controls:EverTreeView.ItemTemplate>
46 
47                 </Controls:EverTreeView>
 1  public class ResourcesSettingViewModel : SoftGuidanceViewModelBase
 2     {
 3         #region Contructor
 4         public ResourcesSettingViewModel()
 5             : this(new ResourcesSettingModel())
 6         {
 7 
 8         }
 9         public ResourcesSettingViewModel(ResourcesSettingModel resourcesSettingModel)
10         {
11             this.resourcesSettingModel = resourcesSettingModel;
12          
13         }
14         #endregion
15         #region  Variables
16 
17         private ResourcesSettingModel resourcesSettingModel;
18 
19         #endregion
20         #region Properties
21         public ResourcesSettingModel ResourcesSettingModel
22         {
23             get { return this.resourcesSettingModel; }
24         }
25         private EverTreeViewItem selectedTreeViewItem;
26       
27 
28         public EverTreeViewItem SelectedTreeViewItem
29         {
30             get { return this.selectedTreeViewItem; }
31             set
32             {
33                 if (this.selectedTreeViewItem == value)
34                 {
35                     return;
36                 }
37                 this.selectedTreeViewItem = value;
38                 this.RaisePropertyChanged(() => this.SelectedTreeViewItem);
39             }
40         }
41 
42        
43         #endregion
44         #region Command
45         public ICommand MenuAddCommand
46         {
47             get
48             {
49                 return new DelegateCommand(()=>
50                     
51                          {
52                              MessageBox.Show("");
53                          });
54             }
55         }
56       
57         
58        
59       
60        
61         #endregion
62 
63         //private EverTreeViewItem GetSelectdTreeViewItem()
64         //{
65         //    EverTreeViewItem treeViewItem = (EverTreeViewItem)SelectedTreeViewItem;
66         //    return treeViewItem;
67         //}
68     }

在绑定menuitem Command 的时候怎么也触发不了,求解???

我用的事prism 框架

栈客的主页 栈客 | 初学一级 | 园豆:192
提问于:2012-11-24 09:56
< >
分享
所有回答(4)
0

Command="{Binding DataContext.MenuAddCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"

 

这里的Source搞错了吧,应该是你在页面中的ResourcesSettingViewModel对象,仔细检查一下~

暴力程序猿 | 园豆:296 (菜鸟二级) | 2012-11-25 14:38

 你是说这样吗  <MenuItem Header="添加"  Command="{Binding ResourcesSettingViewModel.MenuAddCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>

支持(0) 反对(0) 栈客 | 园豆:192 (初学一级) | 2012-11-25 20:11

@Rafferty: 你是说<MenuItem Header="添加"  Command="{Binding MenuAddCommand}/>

支持(0) 反对(0) 栈客 | 园豆:192 (初学一级) | 2012-11-25 20:26

@Rafferty: 你在页面中应该类似于

<Resource>

 <XXX:ResourcesSettingViewModel x:Key="resourcesSettingViewModel" />

</Resource>

这样一个资源吧,  在Command绑定的时候source就是这个资源

如果按照这样来写,那就是 Command={Binding MenuAddCommand, Source={StaticResource resourcesSettingViewModel}}

支持(0) 反对(0) 暴力程序猿 | 园豆:296 (菜鸟二级) | 2012-11-26 09:46
-1

在WPF中TreeView和ContextMenu在视觉树上是分开的也就是,ContextMenu上的DataContext就没有绑定任何ViewModel。

http://stackoverflow.com/questions/3668654/relativesource-binding-from-a-tooltip-or-contextmenu
Lee's Blog | 园豆:530 (小虾三级) | 2013-02-28 09:54
-1

这个问题我已经解决,不知道你还需要解决方案否?如果需要,白天我发个博客来解答。

青林一霸 | 园豆:184 (初学一级) | 2013-11-14 04:42
0

不用命令模式,换个比的方式:吧ContextMenu作为ViewModel中的一个属性,在MenuItem的Click事件里写代码。 

会长 | 园豆:12463 (专家六级) | 2015-12-04 12:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册