私は、EventTriggerからViewModelにViewModelにパラメータを返すための最良の方法であるという人からのアドバイスに基づいて、InvokeDelegateCommandActionクラスをAlexeyZakharov's weblogから使用しています。InvokeDelegateCommandActionを使用したイベントトリガのCommandParameter
ここに私のものがあります。 ViewModelには
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<cmnwin:InvokeDelegateCommandAction
Command="{Binding SelectedExcludedItemChangedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=SelectedItems}" />
</i:EventTrigger>
</i:Interaction.Triggers>
::ビュー(データグリッドが特定されるように)で
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になるたびに、argは常にNULLです....私はここに完全なdoofus? CommandParamterはどこにコマンドargsにリンクされますか?なぜ、ビューは何もコマンドに何も返さないのですか?助けてください。
シンプルなボタンとコマンドで同じことを試しましたか?私は私のDataGridの行を削除するためにこれを使用し、それは正常に動作します。多分InvokeDelegateCommandActionは問題を引き起こしますか? – blindmeis
ボタンアクションとInvokeCommandActionで試してみましょう...しかし、これは後ろのViewコードにマップする必要はありませんか?私はここにコードを持たないという要件があります。 – tigerswithguitars
RelayCommandメソッドを実行しようとしましたが、まだバナナはありません。ある時点でボタンメソッドを試してみます。しかし、私はそれが欲しいと思っていることをしていないということにまだ悩まされています/私は何をすべきかわかりません.... – tigerswithguitars