简体   繁体   English

如何在IsMouseOver触发器上更改UserControl的子元素的属性?

[英]How to change a property of a UserControl's child element on a IsMouseOver trigger?

I have a custom usercontrol which has an outline border which i want to highlight somehow while the mouse is over the control. 我有一个自定义用户控件,该控件具有轮廓边框,当鼠标悬停在控件上时,我想以某种方式突出显示该边框。 I tried to add a style with mouse over trigger to the usercontrol but there didn't find out how to target the child border (style targetname seems not to find "OuterBorder"). 我试图用鼠标悬停触发器向用户控件添加样式,但是没有找到如何定位子边框的样式(样式目标名似乎找不到“ OuterBorder”)。

Adding the trigger to the border itself seems to not work at all. 将触发器添加到边框本身似乎根本不起作用。

<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>

Thanks for any ideas! 感谢您的任何想法!

You have set a Local Value of BorderBrush property to acacac . 您已将BorderBrush属性的Local Value设置为acacac This local value will take precedence. 此本地值将优先。 See Property Value Precedence . 请参阅属性值优先级

  1. Set Background=Transparent for the Border . Border设置Background=Transparent

  2. Remove BorderBrush from Border tag and make following changes : Border标签中删除BorderBrush并进行以下更改:

     <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM