如何在 IsMouseOver 触发器上更改用户控件的子元素的属性
本文关键字:控件 用户 属性 元素 IsMouseOver 触发器 | 更新日期: 2023-09-27 18:32:45
我有一个自定义用户控件,它有一个轮廓边框,我想在鼠标悬停在控件上时以某种方式突出显示。我尝试将鼠标悬停触发器的样式添加到用户控件,但没有找到如何定位子边框(样式目标名称似乎找不到"OuterBorder")。
将触发器添加到边框本身似乎根本不起作用。
<UserControl x:Class="CCC.Controls.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CCC.Controls"
mc:Ignorable="d"
d:DesignHeight="240" d:DesignWidth="182" Height="240" Width="182" Margin="2" >
<!-- tried to add trigger here too as UserControl.Style -->
<Grid>
... <!-- other controls inside here -->
<Border x:Name="OuterBorder" Width="182" CornerRadius="3" BorderThickness="3" BorderBrush="#acacac" Margin="0,00,0,0" Height="240" VerticalAlignment="Top">
<!-- doesnt work -->
<Border.Style>
<Style>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Grid>
</UserControl>
感谢您的任何想法!
您已将BorderBrush
属性的Local Value
设置为 acacac
。此本地值将优先。请参阅属性值优先级。
-
为
Border
设置Background=Transparent
。 -
从标记中删除
BorderBrush
Border
并进行以下更改:<Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="Red"/> </Trigger> <Trigger Property="IsMouseOver" Value="False"> <Setter Property="BorderBrush" Value="#acacac"/> </Trigger> </Style.Triggers>