gpt4 book ai didi

c# - 如何在 Prism 5.0 中为 TreeviewItem 编写 EventToCommand

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:26 26 4
gpt4 key购买 nike

我有 Treeview,它也有子节点并且是动态填充的。 View 的数据上下文绑定(bind)到 View 模型。当我双击 TreeView 的任何级别时,我想获取 TreeView 的选定项。我想为双击事件编写 EventToCommand。我不想在代码后面写任何代码。

TreeView 的结构如下。我已经编写了 InvokeCommandAction 以使用 delegatecommand 将 doubleclick 事件传递给 viewmodel。这是工作。但是当我双击子节点时,我没有得到选定的项目。始终获取根名称。

<TreeView  Name="treeView" Background="Transparent" ItemsSource="{Binding TreeList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<prism:InvokeCommandAction Command="{Binding TreeNodeDoubleClickevent}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}">
</prism:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected,Mode=TwoWay}" />
<Setter Property="IsExpanded" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.IsExpanded, Mode=TwoWay}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type provider:dataprovider}" ItemsSource="{Binding providerList}">
<TextBlock Text="{Binding Name}" Background="{Binding Property.Background}" Tag="{Binding Parent}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>

查看模型代码:

public class VM_TreeView : IVM_TreeView
{
public DelegateCommand<CustomXML> TreeNodeDoubleClickevent { get; set; }

[ImportingConstructor]
public VM_TreeView(IRegionManager regionManager)
: base(regionManager)
{
RegionManager = regionManager;
TreeNodeDoubleClickevent = new DelegateCommand<CustomXML>(OnExecute_TreeNodeDouble_click);
}

public void OnExecute_TreeNodeDouble_click(CustomXML obj)
{

}

}

请帮助实现这一目标。

最佳答案

它对我来说很好用。引用下面的代码。

<Window x:Class="TreeView_Learning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeView_Learning"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:prism="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"
Title="MainWindow" Height="650" Width="525">
<StackPanel>
<TreeView x:Name="tree" Width="500" Height="200" ItemsSource="{Binding NodeList}"
VerticalAlignment="Top" HorizontalAlignment="Center" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<prism:InvokeCommandAction Command="{Binding TreeNodeDoubleClickevent}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}">
</prism:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding NodeName}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>

  public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}

class ViewModel
{
private ObservableCollection<Node> myVar;

public ObservableCollection<Node> NodeList
{
get { return myVar; }
set { myVar = value; }
}

public DelegateCommand<Node> TreeNodeDoubleClickevent { get; set; }

public ViewModel()
{
NodeList = new ObservableCollection<Node>();
Node nd = new Node();
nd.NodeName = "Item 1.1";
Node nd1 = new Node();
nd1.NodeName = "Item 1.2";
Node nd2 = new Node();
nd2.NodeName = "Item 1";
nd2.Children.Add(nd);
nd2.Children.Add(nd1);
NodeList.Add(nd2);
TreeNodeDoubleClickevent = new DelegateCommand<Node>(MouseDoubleClick);
}

private void MouseDoubleClick(Node obj)
{
MessageBox.Show(obj.NodeName);
}
}

class Node
{
private string nodeName;

public string NodeName
{
get { return nodeName; }
set { nodeName = value; }
}

private ObservableCollection<Node> myVar = new ObservableCollection<Node>();

public ObservableCollection<Node> Children
{
get { return myVar; }
set { myVar = value; }
}
}

关于c# - 如何在 Prism 5.0 中为 TreeviewItem 编写 EventToCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com