簡體   English   中英

如何將樣式應用於所有按鈕?

[英]How can I apply a style to all buttons?

如何以及在何處創建一種樣式,使所有具有資源藍色(黃色邊框,藍色背景)的按鈕控件?

也可以將其添加到texbox嗎?

是否有一個集中的位置,因為我希望這種樣式能夠影響應用程序中不同頁面中的按鈕?

在這些情況下,您可以使用Styles

  • 您將在一個類型的多個控件上應用相同的屬性(或成員)
  • 您將使save成為一種理想的狀態,並在以后使用它。

您可以在控件的資源或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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM