2013-04-05 113 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