views:

55

answers:

2

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;
    }
}
A: 
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type cc:UserControl}}}"
decyclone
I have tried that one but get this error:System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Site.UserControls.UserControl', AncestorLevel='1''. BindingExpression:Path=EnlargeCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
Cinaird
and :System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Site.UserControls.UserControl', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'CommandParameter' (type 'Object')
Cinaird
Can you provide some code please? Because it worked on my machine!
decyclone
I have added the code for Lightboxcommand, and that is about it i think. Do you think the problem is in the Lightboxcommand?
Cinaird
Ok i found out what the problem is but no good solution jet.A context menu is not part of the same visual tree. Ancestor bindings dont work as the context menu is not a child of the element it is on. The only solution i'we found is this http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/ but i rather solve the problem without it
Cinaird
A: 

Ok, i Solved it using ElementSpy, to see how elementSpy works look here: http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/

and the 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>
Cinaird