WPF EventTrigger并不总是触发

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF EventTrigger并不总是触发相关的知识,希望对你有一定的参考价值。

我有一个WPF TreeView,我在视觉上定制看起来像这样:

treeview

我定义了一个EventTrigger来对点击做出反应:

<TreeView>            
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction Command="{Binding OpenPartListCommand}" CommandParameter="{Binding ElementName=PartsTreeView, Path=SelectedItem}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem" >
                            <Grid Margin="6,4">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <Border Name="Bd" 
                                        Background="{Binding Background}"
                                        BorderBrush="Black" 
                                        BorderThickness="1.2" 
                                        CornerRadius="0"
                                        MinHeight="35"
                                        Padding="5"     
                                        SnapsToDevicePixels="True"
                                        Margin="{Binding Path=Margin}" >
                                    <Grid>                                            
                                        <Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
                                            <Expander.Header>                                                   
                                                <ContentPresenter ContentSource="Header"                                                                       
                                                                  Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE-80}"  />

                                            </Expander.Header>                                                
                                            <ItemsPresenter />
                                        </Expander>

                                        <ContentPresenter Name="CntPres"
                                                    ContentSource="Header"
                                                    HorizontalAlignment="Center"
                                                    VerticalAlignment="Center"
                                                    Visibility="Collapsed"                                                         
                                                    Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE-80}"  
                                                    />              
                                    </Grid>
                                </Border>
                            </Grid>

                            <ControlTemplate.Triggers>

                                <Trigger Property="TreeViewItem.HasItems" Value="false">
                                    <Setter 
                                          TargetName="Exp" 
                                          Property="Visibility" 
                                          Value="Collapsed" />
                                    <Setter 
                                          TargetName="CntPres" 
                                          Property="Visibility" 
                                          Value="Visible" />            
                                </Trigger>


                                <Trigger Property="TreeViewItem.Name" Value="IsSpecial">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFC3AF"/>
                                </Trigger>
                                <Trigger Property="TreeViewItem.Name" Value="IsNotSpecial">
                                    <Setter Property="Background" TargetName="Bd" Value="#8BADC5"/>
                                </Trigger>


                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
                                </Trigger>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Bd" Property="Background" Value="#FFF0EA"/>
                                </Trigger>

                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <ItemsPanelTemplate.Resources>                                    
                            </ItemsPanelTemplate.Resources>
                            <StackPanel  IsItemsHost="True"   />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </TreeView.Resources>
</TreeView>

问题是单击根元素“Ausführungsvorschrift”只会扩展树,但事件不会被触发。它在单击子元素时触发,并在单击边框内部时触发。但是标题不会触发任何东西。

答案

尝试将ExpandedCollapsed事件的触发器添加到模板中的Expander元素:

<Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Expanded">
            <i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="Collapsed">
            <i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Expander.Header>
        <ContentPresenter ContentSource="Header" />
    </Expander.Header>
    <ItemsPresenter />
</Expander>

以上是关于WPF EventTrigger并不总是触发的主要内容,如果未能解决你的问题,请参考以下文章

WPF 通过EventTrigger修改鼠标样式

WPF 使用EventTrigger设置Item的IsSelected选项

WPF 使用EventTrigger设置Item的IsSelected选项

WPF 使用EventTrigger设置Item的IsSelected选项

wpf 触发器理解

WPF: WPF 中的 Triggers 和 VisualStateManager