周围的边框对鼠标的选择风格鼠标、边框、风格

2023-09-04 01:34:26 作者:Stray(流浪)

我有一个电网边框周围。当鼠标悬停在电网,我想改变风格上的边框。我怎么会去这样做呢?这是我已经试过,没有任何成功至今:

I have a Grid with a Border around it. Upon mouse over on the Grid, I want to change the style on the Border. How would I go about doing this? This is what I've tried, without any success so far:

<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中,我得到的错误

Upon attempting to build this XAML, I get the error

targetName属性不能在风格二传手设定。

TargetName property cannot be set on a Style Setter.

但我想不出任何其他方式来做到这一点。帮助将非常AP preciated。使用任何code-背后,是出了问题。

but I can't think of any other way to do this. Help would be much appreciated. Using any code-behind is out of the question.

推荐答案

您需要做到以下几点:

从边界定义中删除的BorderBrush。触发器可以覆盖由制定者在风格设置属性,而不是直接在标签中设置属性。

Remove the BorderBrush from the Border definition. Triggers can overwrite properties set by setters in styles, but not properties set directly within the tag.

将触发到边境,而不是到Grid(见的例子,其他人提供)。

Put the Trigger into the Border rather than into the Grid (see the examples that the others provided).

至于对电网的点击测试:将一个透明的框格的后面,使鼠标悬停总是包含:

Regarding the hit test on the Grid: Put a transparent box behind the grid so that MouseOver is always captured:

有3点code例如:

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