WPF样式使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27508477/article/details/88092870

当页面中有多个元素的时候,可以通过样式来批量对参数赋值。

<Window x:Class="Effluent.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:Effluent"
        mc:Ignorable="d"
        Title="MainWindow" Height="1080" Width="1920"
        Loaded="Window_Loaded">
    <Window.Resources>
        <LinearGradientBrush x:Key="Brush1" EndPoint="0,0" StartPoint="1,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="White" Offset="0.5"/>
            <GradientStop Color="Black" Offset="1"/>
        </LinearGradientBrush>

        <Style x:Key="ImageStyle">
            <Setter Property="Control.Width" Value="48"/>
            <Setter Property="Control.Height" Value="48"/>
        </Style>

    </Window.Resources>
    <Grid>
        <Canvas Margin="35,160,-35,0" VerticalAlignment="Top">
            <Image  Canvas.Left="60" Canvas.Top="35"  x:Name="Image1" Source="Resources/供料阀开.png" Style="{DynamicResource ImageStyle  }"/>
            ……
            

如上代码,所有需要设置高度为48,长度为48的对象,都可以通过Style="{DynamicResource ImageStyle }这一句代码设置,如果有更多内容,也可以直接卸载Style里面。

猜你喜欢

转载自blog.csdn.net/qq_27508477/article/details/88092870