wpf基础02之组织模板

在上一篇中我们说的控件模板,是定义在当前xaml窗口的静态资源中的,而实际项目中,往往几个窗口需要共用同一控件模板,甚至整个app都可以应用当前此控件模板,所以我们可以在当前app资源字典中定义控件模板。

1.使用资源字典

1.1新建资源字典

右键项目,新建Resources文件夹,在文件夹内新建资源字典,其实资源字典也是xaml文件。

将上一篇中名为btnTemplate的控件模板内容拷贝到资源字典里

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp1.Resources">
    <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
        <Border x:Name="border1" Background="Blue" TextBlock.Foreground="White" BorderThickness="3">
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="border1" Property="Background" Value="Red"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--应用到项目下button的控件模板 -->
    <!--<Style TargetType="{x:Type Button}">
        <Setter Property="Control.Template" Value="{StaticResource btnTemplate}"></Setter>
    </Style>-->

</ResourceDictionary>

1.2合并资源字典

我理解的应该就是将app中定义的资源字典引入到当前xaml页面下,这样当前页面才能使用系统的资源字典。

 <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ButtonStyle.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

 使用方法不变。

 <StackPanel>
            <Button Content="DateTemplate Button1" Template="{StaticResource btnTemplate}"></Button>
        </StackPanel>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏克贝塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值