.net - 在鼠标悬停时更改周围边框的样式

标签 .net wpf xaml styles mouseevent

我有一个 GridBorder周围。将鼠标悬停在 Grid 上,我想更改Border上的样式.我该怎么做呢?这是我尝试过的,到目前为止没有任何成功:

<Border Name="Border" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
    <Grid>
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="#FFB9D7FC" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        ...
    </Grid>
</Border>

在尝试构建此 XAML 时,我收到错误

TargetName property cannot be set on a Style Setter.



但我想不出任何其他方法来做到这一点。帮助将不胜感激。使用任何代码隐藏都是不可能的。

最佳答案

您需要执行以下操作:

  • 从边框定义中删除 BorderBrush。触发器可以覆盖样式中 setter 设置的属性,但不能覆盖标签中直接设置的属性。
  • 将触发器放入边框而不是网格(请参阅其他人提供的示例)。
  • 关于 Grid 上的 HitTest :在网格后面放置一个透明框,以便始终捕获 MouseOver:

  • 第 3 点的代码示例:
    <Grid>
        <Rectangle Fill="Transparent" /><!-- make sure that the mouse is always over "something" --> 
        <Grid Name="myGrid">       
            <Grid.ColumnDefinitions> 
                <ColumnDefinition /> 
                <ColumnDefinition /> 
            </Grid.ColumnDefinitions> 
            <TextBlock Name="myText">sadasdsa</TextBlock> 
        </Grid> 
    </Grid>
    

    关于.net - 在鼠标悬停时更改周围边框的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2145700/

    相关文章:

    .net - 微软问:单列出还是双列出?使用每种方法的利弊是什么?

    c# - 如何使用 .NET 代码执行最困难的 SSL 证书检查?

    .net - 将大量对象长期保留在内存中

    .net - LINQ to Entities 无法识别方法 Int32 get_Item(Int32)

    c# - 将彩色椭圆添加到 MenuItem

    c# - 使用 MessageBox.Show() 阻止所有窗口

    wpf - 在 WPF 中创建类似 Firefox 4 的菜单

    WPF动态布局: how to enforce square proportions (width equals height)?

    c# - 在通过敲击键盘上的 Enter 触发点击事件之前,我可以使用哪个事件来触发对标签的更新?

    xaml - Xamarin 表单 : How to fix Activity Indicator in Centre of Screen in Scrollview