我在Grid
中有以下UserControl
个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>
在单独的Resource Dictionary 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部件,即控件的颜色没有变化? (注意:样式的其他部分正在运作。)
答案 0 :(得分:1)
Grid
对象无法获得焦点(TextBlock
也无法获得焦点,尽管这与上面发布的代码不相关。因此,有理由认为,任何仅在获得焦点时激活的触发器本身都不会被激活。
如果您不这么认为,请提供可靠地重现问题的a good, minimal, complete code example。请务必准确说明用户应如何与程序进行交互,以便您可以激活这些触发器。
答案 1 :(得分:0)
我终于有了这个工作。
对于用户控件:
<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)提供键盘焦点。
总结: