[英]Setting global style in XAML
有什么方法可以在xaml中的資源字典中設置全局樣式,而不必在受影響的元素中指定任何特殊設置?
例如,假設我有resdictionary.xaml文件,
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
然后我有正常的window.xaml文件:
<Window x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="800" MinWidth="500" MinHeight="350">
<Window.Resources>
<ResourceDictionary Source="resdictionary.xaml" />
</Window.Resources>
<Button Content="Button 1"/>
<Button Content="Button 2"/>
<Button Content="Button 3"/>
</Window>
我希望所有按鈕都具有黑白背景漸變色和Calibri字體。 有沒有一種方法可以在resdictionary.xaml中指定它而不必更改window.xaml文件?
您可以創建一個styles.xaml頁面並在其中添加所有樣式
<Style x:Key="SubmitButtonStyle" TargetType="Button">
<Setter Property="Height">
<Setter.Value>28px</Setter.Value>
</Setter>
<Setter Property="Width">
<Setter.Value>90px</Setter.Value>
</Setter>
<Setter Property="BorderThickness">
<Setter.Value>1</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>#000000</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>#0073c6</Setter.Value>
</Setter>
<Setter Property="FontFamily" >
<Setter.Value>Segoe UI</Setter.Value>
</Setter>
<Setter Property="FontSize">
<Setter.Value>12px</Setter.Value>
</Setter>
</Style>
您可以使用以下代碼在Windows中引用styles.xaml。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
您可以按照以下方式在頁面中使用樣式
<Button Style="{StaticResource TelerikRadSubmitButtonStyle}"/>
希望這可以幫助。
您可以按照Dream Coder顯示的方式進行操作。 但是那樣,您必須為每個按鈕設置style
。 如果未將style
設置為按鈕,則該button
將使用默認的Windows樣式。 為此,您必須從style
刪除x:Key
。
<Style TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#000000" />
<GradientStop Color="#FFFFFF"
Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontFamily"
Value="Calibri" />
</Style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.