Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF binding Ancestor object as CommandParameter
    text
    copied!<p>I have a Custom usercontrol that I want to enlarge. I tested this whit a function call on MouseDoubleClick and it worked fine. Code:</p> <pre><code>XAML &lt;cc:UserControl ItemsSource="{Binding Path=DataItem}" MouseDoubleClick="UserControl_MouseDoubleClick" /&gt; CodeBehind c# private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) { MainWindowViewModel.Instance.LightBoxCommand.Execute(sender); } </code></pre> <p>Now i want to do it with MVVM pattern and in a menu like this:</p> <pre><code>XAML &lt;cc:UserControl ItemsSource="{Binding Path=DataItem}" MouseDoubleClick="UserControl_MouseDoubleClick" &gt; &lt;cc:UserControl.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="_Enlarge" Command="{Binding Path=EnlargeCommand, RelativeSource={RelativeSource AncestorType={x:Type cc:UserControl}}}" CommandParameter="{Binding Path=.}" /&gt; &lt;/MenuItem&gt; &lt;/ContextMenu&gt; &lt;/cc:UserControl.ContextMenu&gt; &lt;/cc:UserControl&gt; MVVM C# private ICommand _enlargeCommand; public ICommand EnlargeCommand { get { if (_enlargeCommand == null) _enlargeCommand = new RelayCommand(n =&gt; {MainWindowViewModel.Instance.LightBoxCommand.Execute(n); }); return _enlargeCommand; } } </code></pre> <p>The problem is that I'm not quite sure how to bind to the parent object, i want to send the whole UserControl to the "LightBoxCommand". Any ideas? </p> <p>c# Lightboxcommand</p> <pre><code>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 =&gt; { if (IsLightBoxVisible == true) LightBoxVisible = Visibility.Hidden; else { LightBoxVisible = Visibility.Visible; CurrentLightBoxItem = ((UserControl)n).Copy(); NotifyPropertyChanged("CurrentLightBoxItem"); } IsLightBoxVisible = !IsLightBoxVisible; NotifyPropertyChanged("LightBoxVisible"); }); return _lightBoxCommand; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload