1、新建一个资源文件,比如: Gaugestyle.xaml和MenuButton.xaml
2、在App.xaml中添加该资源:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="spjl1.App"> <Application.Resources> <!-- Resources scoped at the Application level should be defined here. --> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Gaugestyle.xaml"/> <ResourceDictionary Source="MenuButton.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
3、在页面文件后台中使用:
ResourceDictionary resources = new ResourceDictionary(); resources.Source = new Uri("/spjl1;component/MenuButton.xaml", System.UriKind.Relative); RadWindow1.Style = resources["ChildWindowStyle1"] as Style;
=====================================================内容补充======================================================
这里用一个Label的资源样式文件LabelStyle.xaml来讲解:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--添加面板·标题--> <Style x:Key="tbAddTitle" TargetType="{x:Type Label}"> <Setter Property="FontFamily" Value="Microsoft YaHei" /> <Setter Property="Height" Value="28" /> <Setter Property="FontSize" Value="16" /> <Setter Property="Foreground" Value="#FF0000" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ResourceDictionary>
将该资源样式文件添加到App.xaml中
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="spjl1.App"> <Application.Resources> <!-- Resources scoped at the Application level should be defined here. --> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="LabelStyle.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
如果在前台XAML中使用,使用Style属性,方式如下:
<TextBlock Foreground="Black" Margin="0,1,479,0" Name="textBlock1" Style="{StaticResource tbAddTitle}" Text="供货商名称:" Grid.ColumnSpan="2" />