WPF MVVM模式下 实现TreeView控件绑定鼠标双击事件,并且传递当前选择TreeViewItem

本文详细描述了如何在WindowsPresentationFoundation(WPF)项目中,利用Behaviors库为TreeView的MouseDoubleClick事件创建响应,并通过DataBinding绑定到ViewModel中的ShowContent方法。展示了相关代码片段以供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Behaviors工具库,绑定TreeView MouseDoubleClick事件,具体代码如下:

 <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding ShowContentCommand}" CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}" />
      </i:EventTrigger>
</i:Interaction.Triggers>

ShowContentCommand 为ViewModel文件内绑定的方法:

        public void ShowContent(TreeViewItem treeViewItem)
        {
            //解析当前获取到TreeViewItem
        }

详细代码如下:

<Page
    x:Class="Smart.Software.App.Views.Pages.DataReport.PageDataReportAppLog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:Smart.Software.App.Views.Pages.DataReport"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:Smart.Software.App.ViewModels.Pages.DataReport"
    Title="PageDataReportAppLog"
    d:DataContext="{d:DesignInstance Type=vm:PageDataReportAppLogViewModel}"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:CallMethodAction MethodName="Loaded" TargetObject="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Border
        Margin="0,5"
        Background="White"
        Effect="{StaticResource MaterialDesignShadowDepth1}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="5*" />
            </Grid.ColumnDefinitions>
            <Border
                Grid.Column="0"
                Margin="5,0"
                Background="White"
                Effect="{StaticResource MaterialDesignShadowDepth1}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TreeView
                        x:Name="treeView"
                        Grid.Row="1"
                        ItemsSource="{Binding TreeViewContent}"
                        VirtualizingPanel.IsVirtualizing="True">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseDoubleClick">
                                <i:InvokeCommandAction Command="{Binding ShowContentCommand}" CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TreeView>
                </Grid>
            </Border>
            <Border
                Grid.Column="1"
                Margin="5,0"
                Background="White"
                Effect="{StaticResource MaterialDesignShadowDepth1}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBox
                        x:Name="Tbox_SoftwareLog"
                        Grid.Row="1"
                        Margin="5"
                        Padding="2"
                        Background="White"
                        BorderBrush="Transparent"
                        Cursor="Arrow"
                        IsReadOnly="True"
                        ScrollViewer.HorizontalScrollBarVisibility="Auto"
                        ScrollViewer.VerticalScrollBarVisibility="Auto"
                        Style="{StaticResource MaterialDesignOutlinedTextBox}"
                        TextBlock.LineHeight="25"
                        TextWrapping="Wrap" />
                </Grid>
            </Border>
        </Grid>
    </Border>
</Page>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值