私はAlexeyZakharovのウェブログのクラスInvokeDelegateCommandActionを使用しています。これは、ViewからViewModelにEventTriggerからパラメータを送り返すための最良の方法であるという人々からのアドバイスに基づいています。
これが私が持っているものです。
ビュー(具体的にはDataGrid):
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<cmnwin:InvokeDelegateCommandAction
Command="{Binding SelectedExcludedItemChangedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=SelectedItems}" />
</i:EventTrigger>
</i:Interaction.Triggers>
ViewModelの場合:
public DelegateCommandWithParameter SelectedActiveItemChangedCommand
{
get
{
return selectedActiveItemChangedCommand ??
(selectedActiveItemChangedCommand = new DelegateCommandWithParameter(DoSelectedActiveItemsChanged, CanDoSelectedActiveItemsChanged));
}
}
public bool CanDoSelectedActiveItemsChanged(object param)
{
return true;
}
public void DoSelectedActiveItemsChanged(object param)
{
if (param != null && param is List<Object>)
{
var List = param as List<Object>;
MyLocalField = List;
}
}
オブジェクトを引数として渡すことができる新しい種類のDelegateCommand:
public class DelegateCommandWithParameter : ICommand
{
#region Private Fields
private Func<object, bool> canExecute;
private Action<object> executeAction;
private bool canExecuteCache;
#endregion
#region Constructor
public DelegateCommandWithParameter(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
{
bool temp = canExecute(parameter);
if (canExecuteCache != temp)
{
canExecuteCache = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
return canExecuteCache;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
executeAction(parameter);
}
#endregion
}
私のコードがDoSelectedActiveItemsChangedに到達するときはいつでも、引数は常にNULLです....私はここで完全なバカですか?CommandParamterはどこでコマンド引数にリンクされますか?別名、ビューがコマンドに何も返さないのはなぜですか?助けてください。