[英]How to pass Multiple parameters as CommandParameter in InvokeCommandAction in Windows phone
[英]How to pass Multiple parameters as CommandParameter in InvokeCommandAction In WPF App Using MVVM
我正在以以下方式使用System.Windows.interactivity.dll在ViewModel中獲取鼠標事件。
<ListBox x:Name="listBox" ItemsSource="{Binding HeaderList}" DisplayMemberPath="Text" Width="Auto" Margin="0,0,0,300" Height="Auto" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}"
CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
和在ViewModel中。
public class Headers
{
public Headers()
{
IsSelected = false;
}
public string Text { get; set; }
public ListBox Control { get; set; }
public bool IsSelected { get; set; }
}
public ObservableCollection<Headers> HeaderList
{
get { return _headerList; }
set
{
_headerList = value;
base.OnPropertyChanged("HeaderList");
}
}
public ICommand MouseLeftButtonUpCommand { get; set; }
public DesignTemplateViewModel()
{
string file = SessionHelper.FilePath;
List<string> columns = new List<string>();
if (!string.IsNullOrEmpty(file))
{
ExcelHelper Excel = new ExcelHelper(file);
columns = Excel.GetHeader();
}
else
{
columns.Add("Name");
columns.Add("FatherName");
columns.Add("MotherName");
columns.Add("Class");
columns.Add("RollNo");
columns.Add("ModeOfTransport");
columns.Add("Phone");
columns.Add("Mobile");
}
HeaderList = new ObservableCollection<Headers>();
foreach (string column in columns)
{
HeaderList.Add(new Headers
{
Text = column,
});
}
MouseLeftButtonUpCommand = new RelayCommand((item) => OnMouseLeftButtonUp((Headers)item));
}
private void OnMouseLeftButtonUp(Headers sender)
{
ListBox control = sender.Control as ListBox;
DragDrop.DoDragDrop(control, sender.Text, DragDropEffects.Copy);
}
因此,在這里我需要傳遞多個對象,例如生成此事件的控件,與鼠標相關的屬性等。現在,我正在傳遞單個參數,此代碼可以正常工作。 所以我的問題是如何從Xaml(View)傳遞多個參數並在此ViewModel上訪問它們。 任何代碼幫助嗎?
您可以嘗試使用自定義Converter and MultiBinding
<CommandParameter>
<MultiBinding Converter="{StaticResource CustomConverter}">
<Binding ElementName=".." Path=".."/>
<Binding ElementName=".." Path=".."/>
</MultiBinding>
</CommandParameter>
轉換器
class CustomConverter : IMultiValueConverter
{
public object Convert (object[] Values, Type Target_Type, object Parameter, CultureInfo culture)
{
var findCommandParameters = new FindCommandParameters();
findCommandParameters.Property1 = (string)values[0];
findCommandParameters.Property1 = (string)values[1];
return findCommandParameters;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
參量
public class FindCommandParameters
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
我同意阿吉拉斯。 就是這樣做的。 我改進了Aghilas的代碼,以澄清缺少的內容。 請注意,“ i:InvokeCommandAction.CommandParameter”必須放在invokeCommandAction聲明中。
<ListBox x:Name="listBox" ItemsSource="{Binding HeaderList}" DisplayMemberPath="Text" Width="Auto" Margin="0,0,0,300" Height="Auto" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource XAMLResourceAddConverter}">
<Binding ElementName="listBox" Path="SelectedItem"/>
<Binding ElementName="listBox" Path="SelectedItem"/>
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.