I have a Custom usercontrol that I want to enlarge. I tested this whit a function call on MouseDoubleClick and it worked fine. Code:
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);
}
Now i want to do it with MVVM pattern and in a menu like this:
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;
}
}
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?
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;
}
}