WPF связывает объект Ancestor как CommandParameter - PullRequest
1 голос
/ 21 июня 2010

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

Ответы [ 2 ]

1 голос
/ 21 июня 2010
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type cc:UserControl}}}"
0 голосов
/ 22 июня 2010

Хорошо, я решил это с помощью 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...