我在UserControl
中有以下Grid
,在它自己的XAML文件中:
<Grid Style="{StaticResource GridStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="Box1" Style="{StaticResource ContainedTextBlock}" Grid.Column="0"/>
<TextBlock x:Name="Box2" Style="{StaticResource ContainedTextBlock}" Grid.Column="2"/>
<TextBlock x:Name="Box3" Style="{StaticResource ContainedTextBlock}" Grid.Column="4" TextAlignment="Right"/>
</Grid>
在一个单独的资源字典XAML文件中,我有以下内容:
<SolidColorBrush x:Key="PrimaryColour" Color="#FF334D51"/>
<SolidColorBrush x:Key="BackgroundGray" Color="#FFDEDEDE"/>
<SolidColorBrush x:Key="SelectedGray" Color="#FF566164"/>
<Style TargetType="TextBlock" x:Key="ContainedTextBlock">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryColour}"/>
<Setter Property="TextTrimming" Value="WordEllipsis"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="Grid" x:Key="GridStyle">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{StaticResource BackgroundGray}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedGray}"/>
</Trigger>
</Style.Triggers>
</Style>
有人能帮我解释为什么Style。Triggers部分在运行时没有被拾取,即我的控件的颜色没有改变吗?(注意:样式的其他部分正在工作。)
Grid
对象无法接收焦点(TextBlock
也无法接收焦点,尽管这在上面发布的代码中并不直接相关)。因此,任何只有在收到焦点时才被激活的触发器本身都永远不会被激活。
如果您不这么认为,请提供一个好的、最小、完整的代码示例,以可靠地再现问题。请确保包含用户应该如何与程序交互的精确解释,以便您期望这些触发器被激活。
我终于完成了这项工作。
用户控制:
<UserControl x:Class="Project.Controls.CustomGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto" Style="{StaticResource GridStyle}">
<Grid Style="{StaticResource ContainedGrid}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="Block1" Grid.Column="0" Style="{StaticResource ContainedTextBlock}"/>
<TextBlock x:Name="Block2" Grid.Column="2" Style="{StaticResource ContainedTextBlock}"/>
<TextBlock x:Name="Block3" Grid.Column="4" TextAlignment="Right" Style="{StaticResource ContainedTextBlock}"/>
</Grid>
</UserControl>
在资源字典中:
<Style TargetType="UserControl" x:Key="GridStyle">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Focusable" Value="True"/>
</Style>
<Style TargetType="{x:Type Grid}" x:Key="ContainedGrid">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="True">
<Setter Property="Background" Value="{StaticResource BackgroundGray}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsKeyboardFocused, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedGray}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="ContainedTextBlock">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryColor}"/>
<Setter Property="TextTrimming" Value="WordEllipsis"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsKeyboardFocused, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
另一件需要注意的事情,最后一件让我失望的事情,是我用来给用户控件提供键盘焦点的事件处理程序没有…
e.Handled = true;
…最后。
因此,点击会冒泡回来,并使容器(ScrollViewer)成为键盘焦点。
总结:
- 获得焦点的元素是UserControl
- 网格和文本块基于其祖先中最近的UserControl的焦点状态进行格式化