WPF:在资源样式中设置鼠标悬停边框

时间:2017-06-22 08:49:37

标签: wpf xaml wpf-style

我将这种风格作为App.xaml

中的资源
<Style x:Key="StackPanelLink" TargetType="{x:Type StackPanel}">
    <Setter Property="Width" Value="500" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="GhostWhite" />
            <Setter Property="Border.BorderBrush" Value="LightBlue" />
            <Setter Property="Border.BorderThickness" Value="10" />
        </Trigger>
    </Style.Triggers>
</Style>

背景改变触发器有效,但边界改变触发器不起作用。我在网上看到的所有例子都在StackPanel中使用了Border元素,我不明白如何将它应用于资源文件中的样式。

Rant:到目前为止,我真的很讨厌WPF。这是我用过的最不可发现,最不直观的技术。我尝试做的每件小事都是一小时的谷歌搜索和一些涉及30行XML的解决方案。

1 个答案:

答案 0 :(得分:3)

  

背景改变触发器有效,但边界改变触发器不起作用。

那是因为StackPanel上不存在这些属性。 StackPanel没有边框。它只是一个Panel,它将子元素排列成一条可以水平或垂直定向的线。

听起来你想要使用Border元素和Style:

<Style x:Key="StackPanelLink" TargetType="{x:Type Border}">
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Width" Value="500" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="GhostWhite" />
            <Setter Property="Border.BorderBrush" Value="LightBlue" />
            <Setter Property="Border.BorderThickness" Value="10" />
        </Trigger>
    </Style.Triggers>
</Style>
...
<Border Style="{StaticResource StackPanelLink}">
    <TextBlock>....</TextBlock>
</Border>
相关问题