zoukankan      html  css  js  c++  java
  • WPF样式

    样式是改变控件的基础方法
    举例:改变三个Button的外观属性
     
    XAML代码:
    <Window x:Class="样式.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:样式"
            mc:Ignorable="d" Loaded="Window_Loaded"
            Title="MainWindow" Height="300" Width="300">
        <WrapPanel Name="wrapple">
            <Button Content="Button1"/>
            <Button Content="Button2"/>
            <Button Content="Button3"/>
        </WrapPanel>
    </Window>
    改变控件外观的实现方法:
    1.通过代码实现
    在加载Windows窗体时的Window_Loaded事件中改变控件的旋转角度、颜色、边距和高度
    namespace 样式
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                int count = this.wrapple.Children.Count;
                for (int i = 0; i < count; i++)
                {
                    Button btn = wrapple.Children[i] as Button;
                    RotateTransform rotatetransform = new RotateTransform();
                    rotatetransform.Angle = 45;
                    btn.RenderTransform = rotatetransform;
                    btn.Background = new SolidColorBrush(Colors.Gray);
                    btn.Height=50;
                    btn.Margin = new Thickness(35,0,0,0);
                }
            }
        }
    }
     
    2.在XAML中改变
    <Window x:Class="样式.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:样式"
            mc:Ignorable="d"
            Title="MainWindow" Height="300" Width="300">
        <WrapPanel Name="wrapple">
            <Button Content="Button1" Height="50" Margin="35,0,0,0" Background="Gray">
                <Button.RenderTransform>
                    <RotateTransform Angle="45"/>
                </Button.RenderTransform>
            </Button>
            <Button Content="Button2" Height="50" Margin="35,0,0,0" Background="Gray">
                <Button.RenderTransform>
                    <RotateTransform Angle="45"/>
                </Button.RenderTransform>
            </Button>
            <Button Content="Button3" Height="50" Margin="35,0,0,0" Background="Gray">
                <Button.RenderTransform>
                    <RotateTransform Angle="45"/>
                </Button.RenderTransform>
            </Button>
        </WrapPanel>
    </Window>
    存在问题:当Button数量较多时需要不断地复制粘贴。
    解决方法:设置Button的样式
    其中既可以通过关键字Key或TargetStyle设置控件的样式
    (1)TargetType
    <Window x:Class="样式.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:样式"
            mc:Ignorable="d"
            Title="MainWindow" Height="300" Width="300">
        <Window.Resources>
            <Style TargetType="Button">
                <Setter Property="Height" Value="50"/>
                <Setter Property="Margin" Value="35,0,0,0"/>
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform Angle="45"/>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <WrapPanel Name="wrapple">
            <Button Content="Button1"/>
            <Button Content="Button2"/>
            <Button Content="Button3"/>
        </WrapPanel>
    </Window>
     
    (2) Key
    <Window x:Class="样式.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:样式"
            mc:Ignorable="d"
            Title="MainWindow" Height="300" Width="300">
        <Window.Resources>
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="Height" Value="50"/>
                <Setter Property="Margin" Value="35,0,0,0"/>
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform Angle="45"/>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <WrapPanel Name="wrapple">
            <Button Content="Button1" Style="{StaticResource ButtonStyle}"/>
            <Button Content="Button2" Style="{StaticResource ButtonStyle}"/>
            <Button Content="Button3" Style="{StaticResource ButtonStyle}"/>
        </WrapPanel>
    </Window>
     
    样式的继承:
    样式的继承通过BaseOn属性实现,将两个控件共同的属性抽象为一个基类的样式,然后两个控件的样式均继承自这个基类样式
    举例:
    通过基类样式设置Button和RadioButton的样式,其中二者均派生自Control控件
     
    <Window x:Class="样式.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:样式"
            mc:Ignorable="d"
            Title="MainWindow" Height="300" Width="300">
        <Window.Resources>
            <Style  TargetType="Control">
                <Setter Property="Height" Value="50"/>
                <Setter Property="Margin" Value="35,0,0,0"/>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform Angle="45"/>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style BasedOn="{StaticResource {x:Type Control}}" TargetType="Button">
                <Setter Property="Background" Value="Blue"/>
            </Style>
            <Style BasedOn="{StaticResource {x:Type Control}}" TargetType="RadioButton">
                <Setter Property="Background" Value="Red"/>
            </Style>
        </Window.Resources>
        <WrapPanel Name="wrapple">
            <Button Content="Button" />
            <RadioButton Content="RadioButton"/>
        </WrapPanel>
    </Window>
    触发器WPF的触发器有三种分别为:
    (1)属性触发器Trigger:如上所示
    (2)数据触发器DataTrigger:可以绑定普通的.NET属性,不仅仅是依赖属性
    (3)事件触发器EventTrigger:触发路由事件时会被调用

  • 相关阅读:
    web接口文档apidoc的使用
    python 文件重命名
    sort()排序
    JavaScript自定义事件和触发(createEvent, dispatchEvent)
    Sql 2016 安装到sql_shared_mrconfigaction-install-confignonrc-cpu64卡住不动,是什么原因呢?
    基础提供程序在Open上失败
    win10 Hyper-v 虚拟机监控程序灰色 尝试检索虚拟交换机列表时出错【转载】
    相对路径获取文件名
    省市区三级联动
    反射==>不明确的匹配
  • 原文地址:https://www.cnblogs.com/shougoule/p/12734977.html
Copyright © 2011-2022 走看看