[英]How can I apply a style to all buttons?
如何以及在何處創建一種樣式,使所有具有資源藍色(黃色邊框,藍色背景)的按鈕控件?
也可以將其添加到texbox嗎?
是否有一個集中的位置,因為我希望這種樣式能夠影響應用程序中不同頁面中的按鈕?
在這些情況下,您可以使用Styles
:
您可以在控件的資源或ResourceDictionaries
添加此Style
,如下所示:
<Style TargetType="Button">
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="Background" Value="Blue"/>
</Style>
如果定義x:key
,則應明確說明哪個按鈕遵循您的樣式(例如<Button Style="{StaticResource myButtonStyleKey}">
),否則您的樣式將自動應用於按鈕。
編輯:將ResourceDictionary (名為myStyles.xaml
)添加到您的項目(在名為MyResource
的文件夾中)。 這是代碼:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="Background" Value="Blue"/>
</Style>
</ResourceDictionary>
然后在您的App.xaml
添加以下內容:
<Application x:Class="WPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResource/myStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.