为什么MenuItem不发送指定的参数但Button发送

时间:2010-12-02 08:02:37

标签: wpf routed-commands imultivalueconverter

<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();
    }
}

按钮cammand与MenuItem完全相同。当我为MenuItem调试Converter时,values参数包含两个对象:DependencyProperty.UnsetValue(我不知道这是什么)和MyContextMenu对象。

还有我如何将SomeType作为参数传递? 感谢

1 个答案:

答案 0 :(得分:1)

MenuItems存在于主视觉树之外的弹出窗口中,因此与周围元素的名称范围不同,例如Button。尝试绑定时,ElementName绑定无法解析,因为“Me”元素位于MenuItem的名称范围之外。