要設定背景關系:
我有一個內部有多種資料型別的資料網格。對于每種型別,我都有一個自定義資料模板用于詳細資訊以適合我的資料。在詳細資訊中,我有一個帶有文本框和 2 個按鈕的小表單。目標是在單擊按鈕時獲取行的資料和文本框的內容。
目前,我已經使用這些引數創建了一個類,并且我試圖通過這種方式將這個類傳遞給我的命令引數:
<Button Content="Validate" Width="70" Command="{Binding Source={StaticResource btnVmValidateReserved}, Path=ButtonValidateReservedCommand}">
<Button.CommandParameter>
<model:ButtonValidateReservedCommandArgs Item="{Binding Data}" Message="{Binding Path=Text, ElementName=umMasterComment}"/>
</Button.CommandParameter>
</Button>
這是我的課:
public class ButtonValidateReservedCommandArgs : DependencyObject
{
public static readonly DependencyProperty ItemProperty = DependencyProperty.Register("Item", typeof(object), typeof(ButtonValidateReservedCommandArgs));
public object Item
{
get { return GetValue(ItemProperty); }
set { SetValue(ItemProperty, value); }
}
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ButtonValidateReservedCommandArgs));
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue (MessageProperty, value); }
}
}
當我嘗試使用系結時,結果總是 null 。但是當我嘗試使用隨機文本時,它會起作用。
另外,當我嘗試這樣做時,它可以作業:
<Button Content="Validate" Width="70" Command="{Binding Source={StaticResource btnVmValidateReserved}, Path=ButtonValidateReservedCommand}" CommandParameter="{Binding Data}"/>
但是,我需要我的 2 個論點,但我不知道該怎么做。
如果你有任何想法有一個有效的系結,甚至是傳遞我的 2 個引數的另一種解決方案,我會接受的 ??。
謝謝
uj5u.com熱心網友回復:
為了傳遞多個引數,您可以這樣做:
<Button Command="{Binding YourCommand">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource CloneMultiConverter}">
<Binding Path="Argument1"/>
<Binding Path="Argument2"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
public class CloneMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Clone();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
private void YourCommand(object parameter)
{
var values = (object[])parameter;
var firstArgument = (argumentType)values[0];
var secondArgument = (argumentType)values[1];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/448348.html
