2010-06-21 363 views
1

我有一个自定义的用户控件,我想放大。我在MouseDoubleClick上测试了这个函数调用,并且它工作正常。 代码:作为CommandParameter的WPF绑定祖先对象

XAML 
<cc:UserControl ItemsSource="{Binding Path=DataItem}" MouseDoubleClick="UserControl_MouseDoubleClick" /> 

CodeBehind c# 
private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    MainWindowViewModel.Instance.LightBoxCommand.Execute(sender); 
} 

现在我想用MVVM模式,并在这样的菜单来做到这一点:

XAML 
<cc:UserControl ItemsSource="{Binding Path=DataItem}" MouseDoubleClick="UserControl_MouseDoubleClick" > 
    <cc:UserControl.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="_Enlarge" 
         Command="{Binding Path=EnlargeCommand, RelativeSource={RelativeSource AncestorType={x:Type cc:UserControl}}}" CommandParameter="{Binding Path=.}" 
         /> 
      </MenuItem> 
     </ContextMenu> 
    </cc:UserControl.ContextMenu> 
</cc:UserControl> 

MVVM C# 
private ICommand _enlargeCommand; 
public ICommand EnlargeCommand 
{ 
    get 
    { 
     if (_enlargeCommand == null) 
      _enlargeCommand = new RelayCommand(n => {MainWindowViewModel.Instance.LightBoxCommand.Execute(n); }); 
     return _enlargeCommand; 
    } 
} 

的问题是,我不太清楚如何绑定到父对象,我想发送整个UserControl到“LightBoxCommand”。有任何想法吗?

C#Lightboxcommand

public Visibility LightBoxVisible { get; set; } 
public bool IsLightBoxVisible { get; set; } 

public UserControl CurrentLightBoxItem { get; set; } 

private ICommand _lightBoxCommand; 
public ICommand LightBoxCommand 
{ 
    get 
    { 
     if (_lightBoxCommand == null) 
      _lightBoxCommand = new RelayCommand(n => { 
       if (IsLightBoxVisible == true) 
        LightBoxVisible = Visibility.Hidden; 
       else 
       { 
        LightBoxVisible = Visibility.Visible; 
        CurrentLightBoxItem = ((UserControl)n).Copy(); 
        NotifyPropertyChanged("CurrentLightBoxItem"); 
       } 

       IsLightBoxVisible = !IsLightBoxVisible; 
       NotifyPropertyChanged("LightBoxVisible"); 
      }); 
     return _lightBoxCommand; 
    } 
} 

回答

0

好吧,我解决它使用ElementSpy,看看如何工作elementSpy看这里:http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/

和XAML:

<cc:UserControl x:Name="UserControl" ItemsSource="{Binding Path=DataItem}" MouseDoubleClick="UserControl_MouseDoubleClick" > 
     <cc:UserControl.ContextMenu> 
      <ContextMenu local:ElementSpy.NameScopeSource="{StaticResource ElementSpy}"> 
       <MenuItem Header="_Enlarge" Command="{Binding Path=EnlargeCommand}" CommandParameter="{Binding ElementName=UserControl, Path=.}"/> 
       </MenuItem> 
      </ContextMenu> 
     </cc:UserControl.ContextMenu> 
    </cc:UserControl> 
1
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type cc:UserControl}}}" 
+0

我曾尝试那一个,但得到这个错误: System.Windows.Data错误:4:找不到s用于绑定参考'RelativeSource FindAncestor,AncestorType ='Site.UserControls.UserControl',AncestorLevel ='1''。 BindingExpression:路径= EnlargeCommand;的DataItem = NULL;目标元素是'MenuItem'(Name ='');目标属性是'命令'(类型'ICommand') – Cinaird 2010-06-21 08:21:38

+0

和: System.Windows.Data错误:4:无法找到绑定参考的源'RelativeSource FindAncestor,AncestorType ='Site.UserControls.UserControl',AncestorLevel =' 1'。 BindingExpression :(无路径);的DataItem = NULL;目标元素是'MenuItem'(Name ='');目标属性是'CommandParameter'(类型'对象') – Cinaird 2010-06-21 08:22:36

+0

你能提供一些代码吗?因为它在我的机器上工作! – decyclone 2010-06-21 09:48:08