TreeView가 있습니다. 코드 숨김 방식을 사용하여 treeView_Expanded(object sender, RoutedEventArgs e)
이벤트 TreeViewItem.Expanded
를 처리 할 때 두 가지 매개 변수가 있습니다 .
XAML :
<TreeView Name="treeView" TreeViewItem.Expanded="treeView_Expanded"/>
코드 숨김 :
private void treeView_Expanded(object sender, RoutedEventArgs e)
{
//***I take THE SECOND PARAMETER to work ***
TreeViewItem item = e.Source as TreeViewItem;
}
그러나 TreeViewItem.Expanded
MVVM 규칙을 사용하여 이벤트를 처리 할 때 항상 첫 번째 매개 변수를 사용 object sender
합니다. 그러나 두 번째 매개 변수를 사용하고 싶습니다 RoutedEventArgs e
.
XAML :
<TreeView ItemsSource="{Binding Person}">
<i:Interaction.Triggers>
<helper:RoutedEventTrigger RoutedEvent="TreeViewItem.Expanded">
<prism:InvokeCommandAction Command="{Binding GetNewTreeViewItemCommand}"/>
</helper:RoutedEventTrigger>
</i:Interaction.Triggers>
</TreeView>
ViewModel :
public DelegateCommand<RoutedEventArgs> GetNewTreeViewItemCommand { get; set; }
public MainWindowViewModel()
{
GetNewTreeViewItemCommand = new DelegateCommand<RoutedEventArgs>(LoadNewTreeViewITem);
}
private void LoadNewTreeViewITem(RoutedEventArgs e)
{
//e is "object sender"(the FIRST parameter), but I want to take
//RoutedEventArgs e(the SECOND parameter)
}
MVVM에서 이벤트를 처리 할 때 두 번째 매개 변수를 어떻게 보낼 수 있습니까?
다른 TriggerAction을 사용하여 VM에서 명령을 호출 할 수 있습니다.이 명령은 인수도 전달합니다.
<TreeView ItemsSource="{Binding Person}">
<i:Interaction.Triggers>
<helper:RoutedEventTrigger RoutedEvent="TreeViewItem.Expanded">
<local:CustomCommandAction Command="{Binding GetNewTreeViewItemCommand}"/>
</helper:RoutedEventTrigger>
</i:Interaction.Triggers>
CustomCommandAction
public sealed class CustomCommandAction : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(CustomCommandAction), null);
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
protected override void Invoke(object parameter)
{
if (this.AssociatedObject != null)
{
ICommand command = this.Command;
if (command != null)
{
if (this.CommandParameter != null)
{
if (command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
else
{
if (command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
}
}
}
최신 정보
XAML
<TreeView ItemsSource="{Binding Person}" Grid.Row="1" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<i:Interaction.Triggers>
<local:RoutedEventTrigger RoutedEvent="TreeViewItem.Expanded">
<local:CustomCommandAction Command="{Binding GetNewTreeViewItemCommand}"/>
</local:RoutedEventTrigger>
</i:Interaction.Triggers>
</TreeView>
VM
private ObservableCollection<Person> _people = new ObservableCollection<Person>();
public ObservableCollection<Person> Person
{
get { return _people; }
}
private DelegateCommand _getNewTreeViewItemCommand = null;
public ICommand GetNewTreeViewItemCommand { get { return _getNewTreeViewItemCommand; } }
private void LoadNewTreeViewITem(object param)
{
var tuple = (Tuple<object, object>)param;
object sender = tuple.Item1;
RoutedEventArgs e = tuple.Item2 as RoutedEventArgs;
System.Diagnostics.Debug.WriteLine(sender);
System.Diagnostics.Debug.WriteLine(e.RoutedEvent);
}
public MainWindowViewModel()
{
_getNewTreeViewItemCommand = new DelegateCommand(LoadNewTreeViewITem, (o) => true);
for (int i = 0; i < 10; i++)
{
var newPerson = new Person() { Description = i.ToString() };
for (int j = 0; j < 10; j++)
{
newCode.Children.Add(new Person() { Description = i.ToString() + j.ToString() });
}
_people.Add(newCode);
}
}
사람
public class Person
{
public string Description { get; set; }
public ObservableCollection<Person> Children { get; set; } = new ObservableCollection<Person>();
}
명령
public class DelegateCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
RoutedEventTrigger
public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
RoutedEvent _routedEvent;
public RoutedEvent RoutedEvent
{
get { return _routedEvent; }
set { _routedEvent = value; }
}
public RoutedEventTrigger()
{
}
protected override void OnAttached()
{
Behavior behavior = base.AssociatedObject as Behavior;
FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
if (behavior != null)
{
associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
}
if (associatedElement == null)
{
throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
}
if (RoutedEvent != null)
{
associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
}
}
void OnRoutedEvent(object sender, RoutedEventArgs args)
{
base.OnEvent(args);
}
protected override string GetEventName()
{
return RoutedEvent.Name;
}
}
CustomCommandAction
public sealed class CustomCommandAction : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(CustomCommandAction), null);
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
protected override void Invoke(object parameter)
{
if (this.AssociatedObject != null)
{
ICommand command = this.Command;
if (command != null)
{
if (this.CommandParameter != null)
{
if (command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
else
{
if (command.CanExecute(parameter))
{
command.Execute(new Tuple<object, object>(this.AssociatedObject, parameter));
}
}
}
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다