c# wpf Template ContentTemplate

1.概要

1.1 定义内容的外观

2.2 要点分析

2.代码

复制代码
<Window x:Class="WpfApp2.Window1"
        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:WpfApp2"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="template1">
            <TextBlock Text="{Binding}" FontSize="15" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="template2">
            <TextBlock Text="这是模板定义的内容" FontSize="15" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ContentControl ContentTemplate="{StaticResource template1}" 
                        Content="这是控件的内容."/>
        <ContentControl ContentTemplate="{StaticResource template2}"  />
    </StackPanel>
</Window>

3.运行结果

相关推荐
吴可可12329 分钟前
C# CAD二次开发中如何退出窗口
c#
影寂ldy32 分钟前
C# WinForm 三种自定义控件 + 完全自定义重绘控件知识点
开发语言·c#
-银雾鸢尾-1 小时前
C#中的Dictionary相关方法
开发语言·c#
listening7771 小时前
HarmonyOS 6.1 全场景协同实战:从“单机”到“超级终端”的分布式重构
wpf
lzhdim1 小时前
C# 中读取CPU、硬盘和内存温度
开发语言·c#
WarPigs2 小时前
.NET项目app.Config笔记
c#·.net
weixin_4280053017 小时前
C#调用 AI学习从0开始-第3阶段RAG向量数据库-文档切分与入库第15天
人工智能·学习·c#·向量数据库·rag·qdrant·文档切分与入库
心平气和量大福大18 小时前
C#-WPF-布局-Grid
c#·wpf·visual studio
TeamDev19 小时前
JxBrowser 9.3.1 版本发布啦!
java·前端·javascript·c#·混合应用·jxbrowser·浏览器控件