我需要将我的按钮控件从下面替换为Expander控件。
comments_id
我不知道如何将Command添加到Expander控件,因此当展开Expander时,我想使用参数gridAdvanced执行AdvancedCommand。
如何以最好的方式实现这一目标?
感谢。
答案 0 :(得分:0)
使用Expanded
事件:
<Expander Expanded="Expander_Expanded" />
然后从后面的代码中调用命令:
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
ViewModel vm = (ViewModel)DataContext;
if (vm.AdvancedCommand.CanExecute(vm.gridAdvances))
vm.AdvancedCommand.Execute(vm.gridAdvances));
}
将ViewModel
更改为DataContext
所属的任何类型。
答案 1 :(得分:0)
添加对System.Windows.Interactivity.dll
的引用(在Visual Studio中是Project-> Add Reference-> Assemblies-> Extensions),并将交互触发器添加到Expander
:
<Expander xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="IsExpanded" >
<i:InvokeCommandAction Command="{Binding AdvancedCommand}" CommandParameter="{Binding ElementName=gridAdvances}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Expander>
答案 2 :(得分:0)
我建议将mm8的解决方案与Freggar建议的事件 Expanded 一起使用
<UserControl
xmlns:i="clr-amespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"/>
<Expander>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Expanded" >
<i:InvokeCommandAction Command="{Binding AdvancedCommand}" CommandParameter="{Binding ElementName=gridAdvances}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Expander>
<UserControl>
答案 3 :(得分:0)
我遇到了同样的问题,由于某种原因,无法对TreeView对象(.net core 3 + wpf)中的附加属性进行简单的事件触发。
<i:Interaction.Triggers>
<i:EventTrigger EventName="Expander.Expand">
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
我必须通过自定义行为类来解决它。请参见下面的灵感;)
public class TreeViewSelectionBehavior : Behavior<TreeView>
{
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(TreeViewSelectionBehavior), new UIPropertyMetadata(null, OnSelectedItemChanged));
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(TreeViewSelectionBehavior), null);
private static void OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var item = e.NewValue as TreeViewItem;
if (item != null)
{
item.SetValue(TreeViewItem.IsSelectedProperty, true);
}
}
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
this.AssociatedObject.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(this.Expanded));
}
private void Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (this.Command.CanExecute(item))
{
this.Command.Execute(item);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (this.AssociatedObject != null)
{
this.AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
this.AssociatedObject.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(this.Expanded));
}
}
private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
this.SelectedItem = e.NewValue;
}
}
然后用法很简单
<i:Interaction.Behaviors>
<local:TreeViewSelectionBehavior SelectedItem="{Binding SelectedItem}" Command="{Binding ExpandHistoricalItemCommand}"/>
</i:Interaction.Behaviors>