Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中使用PreviewMouseLeftButtonDown事件忽略鼠标单击TreeViewItem扩展器?_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 如何在WPF中使用PreviewMouseLeftButtonDown事件忽略鼠标单击TreeViewItem扩展器?

C# 如何在WPF中使用PreviewMouseLeftButtonDown事件忽略鼠标单击TreeViewItem扩展器?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我的应用程序中有一个树状视图 <TreeView ... <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{B

我的应用程序中有一个树状视图

        <TreeView

            ...

            <TreeView.ItemContainerStyle>
                <Style 
                    TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                    <Setter Property="Margin" Value="0, 2"/>
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnMouseLeftButtonClicked"/>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.ItemTemplate>

                <HierarchicalDataTemplate 
                    ItemsSource="{Binding Children}">

                    <StackPanel 
                        Orientation="Horizontal">
                        <Image 
                            VerticalAlignment="Center"
                            Width="20"
                            Height="20"
                            Source="{Binding Type, Converter={StaticResource ItemTypeToImageConverter}}"/>
                        <TextBlock 
                            FontSize="16"
                            Foreground="White"
                            FontFamily="{StaticResource Dosis}"
                            Margin="10, 0"
                            VerticalAlignment="Center" 
                            Text="{Binding Name}"/>
                    </StackPanel>

                </HierarchicalDataTemplate>

            </TreeView.ItemTemplate>

        </TreeView>

我只想在用户单击树项目内容时执行处理程序中定义的一些操作,但在单击expander时不执行。如何区分点击源?

至于我,我对XAML使用了稍微不同的结构,我将其粘贴,以便您可以根据您的情况调整它:

<TreeView BorderThickness="0" ItemsSource="{Binding RepereTree}" Width="200" Background="Transparent">
    <TreeView.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </TreeView.Resources>
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TreeViewItem  ItemsSource="{Binding ListeSubReperes}">
                <TreeViewItem.Header>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Background="Transparent" Foreground="#FF042271" Text="{Binding NameOri}" HorizontalAlignment="Left" VerticalAlignment="Center" MouseMove="mouseOverNameRepere" ToolTip="{Binding Path=ToolTipModifications}" MouseDown="TreeView_Main"/>
                    </Grid>
                </TreeViewItem.Header>
                <TreeViewItem.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="-20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Background="{StaticResource WindowBackgroundColor}" Foreground="#FF042271" Text="{Binding Name}" Margin="10,0,0,0" HorizontalAlignment="Left" Tag="{Binding IdRepereOri}" VerticalAlignment="Center" Grid.Column="0" MouseDown="TreeView_Sub"/>
                            <!--<TextBlock Foreground="#FF042271" Text="{Binding IdRepereOri}" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center"  Grid.Column="1"/>-->
                        </Grid>
                    </DataTemplate>
                </TreeViewItem.ItemTemplate>
            </TreeViewItem>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

如您所见,我设置了两个不同的函数,分别是关于用户单击项目还是子项目的
MouseDown=“TreeView\u Main”
MouseDown=“TreeView\u Sub”

在您的情况下,删除
“TreeView_Sub”
就足够了,这是我找到的一个简单解决方案。我已将HierarchycalDataTemplate内容包装在边框中,如下面的代码所示:

                <HierarchicalDataTemplate
                    ItemsSource="{Binding Children}">

                    <Border
                        Background="Transparent"
                        BorderThickness="0"
                        MouseDown="OnMouseLeftButtonClicked">
                        <StackPanel 
                            Orientation="Horizontal">
                        <Image 
                            VerticalAlignment="Center"
                            Width="20"
                            Height="20"
                            Source="{Binding Type, Converter={StaticResource ItemTypeToImageConverter}}"/>
                        <TextBlock 
                            FontSize="16"
                            Foreground="White"
                            FontFamily="{StaticResource Dosis}"
                            Margin="10, 0"
                            VerticalAlignment="Center" 
                            Text="{Binding Name}"/>
                        </StackPanel>
                    </Border>

                </HierarchicalDataTemplate>


然后,您只需将发件人强制转换为Border,然后使用其属性处理事件。

谢谢您的回答。我不能投票(声誉不够),但它对我很有用,因为你指出我的解决方案过于复杂。我的解决方案位于下面的答案:)这是最重要的:)
                <HierarchicalDataTemplate
                    ItemsSource="{Binding Children}">

                    <Border
                        Background="Transparent"
                        BorderThickness="0"
                        MouseDown="OnMouseLeftButtonClicked">
                        <StackPanel 
                            Orientation="Horizontal">
                        <Image 
                            VerticalAlignment="Center"
                            Width="20"
                            Height="20"
                            Source="{Binding Type, Converter={StaticResource ItemTypeToImageConverter}}"/>
                        <TextBlock 
                            FontSize="16"
                            Foreground="White"
                            FontFamily="{StaticResource Dosis}"
                            Margin="10, 0"
                            VerticalAlignment="Center" 
                            Text="{Binding Name}"/>
                        </StackPanel>
                    </Border>

                </HierarchicalDataTemplate>