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

标签 c# wpf mvvm treeview prism

我有 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/

相关文章:

wpf - 如何解决 WPF 中的 "Binding Expression Path error"?

vb.net - 如何扩展 ComboBox 以支持命令 (MVVM)?

c# - 使用 prism 5.0 开发 wpf 插件

c# - 为什么我在调用 DbDataAdapter.Update 时使用 ODP.NET OracleDataAdapter 而不是使用 System.Data.OracleClient 的适配器获得 OracleTruncateException?

wpf - 代码风格的访问控制

c# - 在 ComboBox 中显示缩短的文件路径,但在 ComboBox 下拉列表中显示完整路径

wpf - 防止用户选择选项卡 WPF 选项卡项

c# - 在 NUnit 2.5.8 中使用 TestContext 时出现 NullReferenceException

c# - Int 无法保存 int.MaxValue 的值

c# - 统一: How to find all neighbors recursively (neighbors of neighbors)?