WPF中样式的设置

WPF中样式设定

1.在App.xaml文件中定义,
        <!--按钮样式-->

        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">

            <Setter Property="Foreground" Value="Red"/>

            <Setter Property="Background" Value="Silver"/>

            <Setter Property="Height" Value="23"/>

        </Style>

        <!--TextBox样式,这样写可以应用自动所有TextBox-->

        <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">

            <Setter Property="Foreground" Value="Red"/>

        </Style>

这是两种不同的设置方式,对于ButtonStyle需要手工指定需要绑定此样式的Control,可以参见下面的绑定代码;但是对于TextBox这个样式可以应用于整个项目的所有TextBox控件。

手工绑定样式代码:

<Button  Margin="54,0,0,14" Name="button1" VerticalAlignment="Bottom" Style="{StaticResource ButtonStyle}" Click="button1_Click" IsDefault="True" Height="23" HorizontalAlignment="Left" Width="51">登陆(_L)</Button>

或者

button2.Style = (Style)this.TryFindResource("ButtonStyle");

 

 

2.合并资源字典
    <Application.Resources>

        <!-- Resources scoped at the Application level should be defined here. -->

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>

                        <ResourceDictionary Source="Styles/Button.xaml"/>

                        <ResourceDictionary Source="Styles/ComboBox.xaml"/>

                        <ResourceDictionary Source="Styles/Shared.xaml"/>

                        <ResourceDictionary Source="Styles/TextBox.xaml"/>

                        <ResourceDictionary Source="Styles/ListView.xaml"/>

                        <ResourceDictionary Source="Styles/GroupBox.xaml"/>

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>

然后就是创建资源字典了,Add Items资源字典这个模板。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值