因此,我嘗試使用 ResourceDictionary 創建按鈕樣式(背景不透明度為黑色,alpha 為 20%,文本的默認顏色更改為透明白色)。我確實將檔案包含到 App.xaml 中,例如:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
和按鈕正在應用 x:KeyStyle="{StaticResource TopBarButtons}"
所以我的風格看起來像(隨機顏色只是為了測驗):
<Style x:Key="TopBarButtons" TargetType="{x:Type Button}">
<Setter Property="Background" Value="HotPink"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" Padding="5">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="Background" Value="Lime" />
</Trigger>
</Style.Triggers>
</Style>
但是這些都沒有檢測到,根本沒有任何變化。我的錯誤是什么?
uj5u.com熱心網友回復:
環境
<Button ... Background="Transparent"/>
直接在 Button 上設定一個所謂的local value,它的優先級高于 Style Setter 或 Style Trigger 中的 Setter 設定的值。
任何應該由樣式觸發器更改的值只能由樣式設定器初始化,例如
<Setter Property="Background" Value="Transparent"/>
有關參考,請參閱依賴屬性值優先級。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/481273.html
下一篇:如何更好地為另一個班級創建財產
