Home >Backend Development >C#.Net Tutorial >Four ways to set control styles in UWP

Four ways to set control styles in UWP

一个新手
一个新手Original
2017-10-19 10:17:312424browse

1.Implicit method, by specifying only the TargetType of Style. (Set all Button styles)


1 <Page.Resources >
2         <Style TargetType="Button">
3             <Setter Property="BorderBrush" Value="Lime"/>
4             <Setter Property="BorderThickness" Value="4"/>
5         </Style>
6     </Page.Resources>

2.Explicit method, by specifying the TargetType and x:Key attributes of Style, and then Set the target control's Style property by using a {StaticResource} tag extension reference with an explicit key


<Page.Resources >
        <Style x:Key="btnStyle" TargetType="Button">
            <Setter Property="BorderBrush" Value="Lime"/>
            <Setter Property="BorderThickness" Value="4"/>
        </Style>
 </Page.Resources>

//调用
<Button Content="跳转方法" x:Name="btnTest" Style="{StaticResource btnStyle}"/>

3.A single style representation


//1.App.xaml配置文件中
<Application.Resources>
     <SolidColorBrush x:Key="BlueBrush" Color="#FF1C90D1"/>
</Application.Resources>

//2.页面中绑定值MainPage.xaml
<Rectangle Height="2" Width="18" Fill="{StaticResource EggshellBrush}"/>

//3.获取值MainPage.xaml.cs
App.Current.Resources["EggshellBrush"] as SolidColorBrush

4.Use style files to adjust the style

1) Create the folder Themes, right-click and add a new item visual CxamlàResource dictionary style.xaml

2) Write styles in style.xaml such as

<Style TargetType="Button" x:Key="gft_FormBtm">
        <Setter Property="Background" Value="OrangeRed"></Setter>
        <Setter Property="Height" Value="50"></Setter>
        <Setter Property="FontSize" Value="16"></Setter>
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        <Setter Property="MinWidth" Value="300"></Setter>
 </Style>

3) Specify resources in the App.xaml file


<!--4.使用样式文件-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/style.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

4) Use style files in the xaml interface

1 <Button x:Name="btnSubmit"  Content="同意以上协议并注册" HorizontalAlignment="Center" Click="btnSubmit_Click" Style="{StaticResource gft_FormBtm}" />

The above is the detailed content of Four ways to set control styles in UWP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn