2013-04-05 36 views
0

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

它是否也可以添加到一個texbox?

是否有一個集中的地方,因爲我希望這種風格能夠影響我的應用中不同頁面中的按鈕?

回答

4

在這種情況下,你可以使用Styles

  • 你要申請相同的屬性(或構件)上一類
  • 你要做出保存好幾個控制和預定的狀態鍵入並稍後使用它。

您可以添加此Style控制的資源或ResourceDictionaries這樣的:

<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> 
+0

如何將它添加到ResourceDictionaries? – Jason94 2013-04-05 10:43:46

+1

要編輯任何控件模板(整個模板!),只需在設計器或文檔大綱窗口中選擇控件,右鍵單擊並選擇編輯模板。正如Hossein所說,刪除x:key屬性會在該目標類型的所有控件上設置樣式。 How-to by images here:http://www.irisclasson.com/2012/07/22/example-winrtmetro-app-how-to-edit-default-template-in-visual-studio-2012-and-blend / – 2013-04-05 10:44:03

相關問題