[英]How to apply my template in XAML button style while keeping background color?
我在Styles.xaml
文件中具有以下樣式:
<Style x:Key="MainViewButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<SymbolIcon Symbol="{TemplateBinding Content}" Foreground="#FFF7F7F7" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#FF55677F"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Height" Value="40"/>
</Style>
當我啟動項目時,將應用模板,因為按鈕包含其Content
SymbolIcon,但即使以模板后的樣式指定了它們,它們也沒有任何背景色。 它們只是透明的。 怎么會?
您的控件模板將覆蓋您設置的任何樣式。 如果您的ControlTemplate
不遵守樣式,則您不會獲得任何設置。
您可以將背景的Setters和其他Setters放在Template setter的上方,然后使用TemplateBinding
在SymbolIcon
內部設置屬性。 如果您的SymbolIcon沒有任何背景屬性,a,您將無法這樣做。
但是,您可以使用Border並將SymbolIconIn放入其中。 這是我的處理方式:
<Style x:Key="MainViewButton" TargetType="Button">
<Setter Property="Background" Value="#FF55677F"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}">
<SymbolIcon Symbol="{TemplateBinding Content}" Foreground="#FFF7F7F7" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.