我想創建一個在滑鼠懸停時動態更改其樣式的按鈕。我想更改前景、背景、邊框、FontWeight 和/或 FontStyle。我的 Window.Resources 中有以下 XAML...
<Window.Resources>
<Style TargetType="Button" x:Key="mouseOverStyle">
<Setter Property="Foreground" Value="#FF808080" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="TextBlock.FontWeight" Value="Normal" />
<Setter Property="TextBlock.FontStyle" Value="Normal" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" >
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="2" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers >
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#FF303030" />
<Setter Property="TextBlock.FontWeight" Value="Bold" />
<Setter Property="TextBlock.FontStyle" Value="Italic" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
在 MouseOver 上,背景、前景和邊框屬性正確更改。但是,FontWeight 和 FontStyle 不會改變。
請告訴我我缺少什么,或者我需要改變什么。謝謝。
uj5u.com熱心網友回復:
鑒于您定義樣式的方式,您需要明確告訴Button使用哪種樣式,如下所示:
<Button Style="{StaticResource mouseOverStyle}" />
您可以使用一些替代方法,它們會隱式地告訴應用程式中的所有控制元件將樣式應用于它們。讓我們知道,答案可以擴展為包含此方法作為替代方法。
uj5u.com熱心網友回復:
其實……別介意。
它確實有效。
我愚蠢地在隨后的 Button 定義中重新定義了 FontWeight 和 FontStyle。一旦我洗掉了它,動態行為就像我想要的那樣作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/352600.html
下一篇:C#WPF資料觸發器
