4

xaml の commandparameter を介してコマンドにサーバール パラメータを渡したいです。

<i:InvokeCommandAction Command="{Binding HideLineCommand, ElementName=militaryLineAction}"
                       CommandParameter="{Binding ID, ElementName=linesSelector}"/>

上記のサンプルでは、​​ID 変数の横に他の変数をコマンドに渡したいと考えています。どうすれば達成できますか?まことにありがとうございます。

4

1 に答える 1

7

コンバーターでMultiBindingを使用できます。

この例を確認してください。

Person クラスがあるとします。

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

そして、このクラスをコマンド パラメーターとして使用します。

XAML は次のようになります。

<Button Content="Start"
                DataContext="{Binding SourceData}"
                >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding SendStatus, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
                        <i:InvokeCommandAction.CommandParameter>
                            <MultiBinding Converter="{StaticResource myPersonConverter}">
                                <MultiBinding.Bindings>
                                    <Binding Path="Name" />
                                    <Binding Path="Age" />
                                </MultiBinding.Bindings>
                            </MultiBinding>
                        </i:InvokeCommandAction.CommandParameter>
                    </i:InvokeCommandAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

SourceDataPerson オブジェクトはどこにありますか。

そしてmyPersonConverter、PersonConverter オブジェクトです。

public class PersonConverter : IMultiValueConverter
    {   
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null && values.Length == 2)
            {
                string name = values[0].ToString();
                int age = (int)values[1];

                return new Person { Name = name, Age = age }; 
            }
            return null;
        }

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

コマンドでは、 Person オブジェクトをパラメーターとして使用できます。

    public ICommand SendStatus { get; private set; }
    private void OnSendStatus(object param)
    {
        Person p = param as Person;
        if (p != null)
        {

        }
    }
于 2012-11-14T18:41:08.770 に答える