开发者

Why MenuItem doesn't send specified parameters but Button send

开发者 https://www.devze.com 2023-01-28 17:11 出处:网络
<MenuItem Command=\"local:CommandLibrary.RegisterServiceCommand\"> <MenuItem.CommandParameter>
<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消