Menuitemが指定されたパラメーターを送信しないのはなぜボタン送信されます

StackOverflow https://stackoverflow.com/questions/4332895

質問

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

ボタンカムマンドはメニューエムとまったく同じです。 Menuitem用のConverterをデバッグすると、Valuesパラメーターには2つのオブジェクトが含まれます。DependencyProperty.UnsetValue(これは何ですか)とMyContextMenuオブジェクトです。

また、パラメーターとしてsometypeを渡すにはどうすればよいですか?ありがとう

役に立ちましたか?

解決

Menuitemsは、メインの視覚ツリーの外側にあるポップアップに存在するため、ボタンのように周囲の要素と同じ名前の範囲を持っていません。バインドしようとすると、「ME」要素がMenuitemの名前の範囲の外側にあるため、ElementNameバインディングは解決できません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top