<MenuItem Command="local:CommandLibrary.RegisterServiceCommand">
<MenuItem.CommandParameter>
<MultiBinding Converter="{StaticResource TrayWindowViewModelConverterResource}">
<MultiBinding.Bindings>
<Binding ElementName="Me" />
<Binding FallbackValue="Parser" />
</MultiBinding开发者_StackOverflow社区.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
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.
精彩评论