wpf之样式

 看下面代码:

复制代码
<Window x:Class="MyWpf.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:MyWpf"
        mc:Ignorable="d" 
        Title="MainWindow" Height="250" Width="500">
    <Grid  >
        <Button Content="Button" Foreground="Red" HorizontalAlignment="Left" Margin="223,20,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button"  Foreground="Red" HorizontalAlignment="Left" Margin="223,65,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Button"   Foreground="Red" HorizontalAlignment="Left" Margin="223,110,0,0" VerticalAlignment="Top" Width="75"/>
       
    </Grid>
</Window>
复制代码

 

界面:

 

上面代码中Button标签中属性的值都是一样的,重复的,所以我们可以用下面方法来做更改,下面还展示了继承的用法:

复制代码
<Window x:Class="MyWpf.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:MyWpf"
        mc:Ignorable="d" 
        Title="MainWindow" Height="250" Width="500">
    <Window.Resources>
        <!--表示这个样式只针对于Button标签,这个样式的key值是BaseStyle,
        -->
        <Style TargetType="Button" x:Key="BaseStyle">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="VerticalAlignment" Value="Top"></Setter>
            <Setter Property="Width" Value="75"></Setter>
        </Style>
        <!--表示这个样式继承于BaseStyle-->
        <Style x:Key="style1" TargetType="Button" BasedOn="{StaticResource ResourceKey=BaseStyle}" >
            <Setter Property="Content" Value="hello"></Setter>
        </Style>
    </Window.Resources> 
    <Grid>
        <Button Style="{StaticResource ResourceKey=style1}"   Margin="223,20,0,0"   />
        <Button Style="{StaticResource ResourceKey=style1}"  Margin="223,65,0,0" />
        <Button Style="{StaticResource ResourceKey=style1}"   Margin="223,110,0,0"  />

    </Grid>
</Window>
复制代码

 

posted @   安静点--  阅读(24)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示