WPF EventTrigger并不总是触发

问题描述 投票:3回答:1

我有一个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”只会扩展树,但事件不会被触发。它在单击子元素时触发,并在单击边框内部时触发。但是标题不会触发任何东西。

c# wpf mvvm treeview
1个回答
6
投票

尝试将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>
© www.soinside.com 2019 - 2024. All rights reserved.