作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个我自定义的 WPF 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”只会伸展树(Splay Tree),但不会触发事件。虽然它在单击子元素时触发,但在单击边框内时触发。但是 header 不会触发任何东西。
最佳答案
尝试将 Expanded
和 Collapsed
事件的触发器添加到模板中的 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>
关于c# - WPF EventTrigger 并不总是触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819395/
我是一名优秀的程序员,十分优秀!