使用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>