У меня есть пользовательский элемент управления, который я хочу увеличить. Я протестировал этот вызов функции на MouseDoubleClick, и он работал нормально.
Код:
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;
}
}