0
<MenuItem Command="local:CommandLibrary.RegisterServiceCommand">
    <MenuItem.CommandParameter>
        <MultiBinding Converter="{StaticResource TrayWindowViewModelConverterResource}">
            <MultiBinding.Bindings>
                <Binding ElementName="Me" />
                <Binding FallbackValue="Parser" />
            </MultiBinding.Bindings>
        </MultiBinding>
    </MenuItem.CommandParameter>
</MenuItem>

public class TrayWindowViewModelConverter : IMultiValueConverter {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        var viewModel = new Window1ViewModel();

        foreach (var obj in values) {
            if (obj is Window)
                viewModel.Caller = obj as Window;
            else if (obj is string)
                viewModel.ServiceName = obj.ToString();
        }

        return viewModel;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

Button cammand is exactly same as MenuItem. when i debug Converter for MenuItem, values parameter contains two object: DependencyProperty.UnsetValue (I'm not aware what's this) and MyContextMenu object.

And also how can i pass SomeType as parameter? Thanks

Sadegh
  • 4,181
  • 9
  • 45
  • 78

1 Answers1

1

MenuItems exist in popups that are outside the main visual tree and so don't have the same name scope as surrounding elements, like your Button. When trying to bind, the ElementName binding can't resolve because the "Me" element is outside the MenuItem's name scope.

John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • For situations like this you should try to rely on data rather than UI elements. Rather than passing a Window instance to your view model, try passing the Window's view model or some part of it. Doing that will not only separate your V-VM layers but can allow you to take advantage of the inherited DataContext (which is still passed through to MenuItems and ToolTips) for your Binding. – John Bowen Dec 03 '10 at 13:32