[UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

看不見的法師
发布: 2025-08-28 10:41:01
原创
545人浏览过

uwp的用户界面主要由布局容器和内容控件(contentcontrol)组成。布局容器包括grid、stackpanel等,这些类继承自panel,可以容纳多个子元素。相比之下,contentcontrol只能包含一个子元素。

在UWP中,Button、CheckBox、ScrollViewer、Frame、ToolTip等控件都继承自ContentControl。其它控件要么在ContentTemplate中使用ContentControl,要么被ContentControl使用,可以说ContentControl是UWP中最重要的控件之一。

ContentControl的定义并不复杂,它主要包含四个属性:Content、ContentTemplate、ContentTemplateSelector和ContentTransitions。

  1. Content:Content属性可以接受任何类型的值,它的值即为ContentControl要显示的对象。Content的类型可以分为两类:

    • 未继承自UIElement的类型:ContentControl会调用这些类的ToString()方法获取文本并显示。
    • 继承自UIElement的类型:ContentControl会直接将它显示在UI上。
    <StackPanel>
        <ContentControl>
            <AdaptiveTrigger></AdaptiveTrigger>
        </ContentControl>
        <ContentControl>
            <Rectangle Fill="Red" Height="50"></Rectangle>
        </ContentControl>
    </StackPanel>
    登录后复制

    [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

  2. ContentTemplate:要按自己的想法显示ContentControl的内容,可以使用ContentTemplate属性

    public DataTemplate ContentTemplate { get; set; }
    登录后复制
    。DataTemplate定义了如何显示绑定的数据对象的XAML标记。DataTemplate中元素的DataContext相当于所在ContentControl的Content。

    下面的示例展示了如何将ScoreModel显示在UI上。

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <DataTemplate x:Key="PassTemplate">
                <Border Background="Green">
                    <TextBlock FontSize="20" Foreground="White" HorizontalAlignment="Center" Margin="20" Text="{Binding Score}"></TextBlock>
                </Border>
            </DataTemplate>
        </Grid.Resources>
        <ContentControl ContentTemplate="{StaticResource PassTemplate}">
            <ScoreModel Score="30"></ScoreModel>
        </ContentControl>
    </Grid>
    登录后复制

    [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

    AiPPT模板广场
    AiPPT模板广场

    AiPPT模板广场-PPT模板-word文档模板-excel表格模板

    AiPPT模板广场 147
    查看详情 AiPPT模板广场
  3. ContentTemplateSelector:如果需要根据Content动态选择要使用的ContentTemplate,可以使用

    public DataTemplateSelector ContentTemplateSelector { get; set; }
    登录后复制
    属性。

    要使用ContentTemplateSelector,首先需要实现一个继承自DataTemplateSelector的类,并重写

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    登录后复制
    函数,在此函数中返回选中的DataTemplate。

    以下示例展示了SimpleDataTemplateSelector的功能,它通过判断Score是否大于60来选择返回PassTemplate或FailTemplate。PassTemplate和FailTemplate都是SimpleDataTemplateSelector的公共属性,并在XAML中注入到SimpleDataTemplateSelector中。

    public class SimpleDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate PassTemplate { get; set; }
        public DataTemplate FailTemplate { get; set; }
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            var model = item as ScoreModel;
            if (model == null)
                return null;
            if (model.Score >= 60)
                return PassTemplate;
            else
                return FailTemplate;
        }
    }
    登录后复制
    <StackPanel>
        <StackPanel.Resources>
            <DataTemplate x:Key="PassTemplate">
                <Border Background="Green">
                    <TextBlock FontSize="20" Foreground="White" HorizontalAlignment="Center" Margin="20" Text="{Binding Score}"></TextBlock>
                </Border>
            </DataTemplate>
            <DataTemplate x:Key="FailTemplate">
                <Border Background="Red">
                    <TextBlock FontSize="20" Foreground="White" HorizontalAlignment="Center" Margin="20" Text="{Binding Score}"></TextBlock>
                </Border>
            </DataTemplate>
            <SimpleDataTemplateSelector FailTemplate="{StaticResource FailTemplate}" PassTemplate="{StaticResource PassTemplate}" x:Key="DataTemplateSelector"></SimpleDataTemplateSelector>
            <Setter Property="ContentTemplateSelector" Value="{StaticResource DataTemplateSelector}"></Setter>
        </StackPanel.Resources>
        <ContentControl>
            <ScoreModel Score="60"></ScoreModel>
        </ContentControl>
        <ContentControl>
            <ScoreModel Score="30"></ScoreModel>
        </ContentControl>
    </StackPanel>
    登录后复制

    [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

  4. ContentTransitions

    public TransitionCollection ContentTransitions { get; set; }
    登录后复制
    是一个Transition类型的集合,提供Content改变时的过渡动画。

    <ContentControl x:Name="ContentControl">
        <ContentControl.ContentTransitions>
            <TransitionCollection>
                <AddDeleteThemeTransition></AddDeleteThemeTransition>
            </TransitionCollection>
        </ContentControl.ContentTransitions>
    </ContentControl>
    登录后复制

    UWP提供了许多优秀的动画效果,适当使用可以提升用户体验。如果没有优秀的UI设计,使用默认的ContentTransitions也是不错的选择。

以上就是[UWP 自定义控件]了解模板化控件(2.1):理解ContentControl的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号