[源码下载]

背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page

作者:webabcd

介绍
背水一战 Windows 10 之 控件(控件基类 - ContentControl, UserControl, Page)

  • ContentPresenter
  • ContentControl
  • UserControl
  • Page

示例
1、演示 ContentPresenter 的基础知识
Controls/BaseControl/ContentControlDemo/ContentPresenterDemo.xaml

<Page
x:Class="Windows10.Controls.BaseControl.ContentControlDemo.ContentPresenterDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.ContentControlDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <ContentControl Width="400" Margin="5" Content="我是 ContentControl" HorizontalAlignment="Left">
<ContentControl.Template>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<Grid Background="Yellow">
<!--
ContentPresenter - 用来呈现 ContentControl 的 Content
有一堆属性,相关说明请参见文档
-->
<ContentPresenter HorizontalAlignment="Right" Foreground="Black" FontSize="24" Padding="20" />
</Grid>
</Border>
</ControlTemplate>
</ContentControl.Template>
</ContentControl> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/ContentControlDemo/ContentPresenterDemo.xaml.cs

/*
* ContentPresenter - 用来呈现 ContentControl 的 Content(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
*
*
* 注:关于如何创建自定义的 ContentPresenter 请参见 /Controls/CollectionControl/ItemsControlDemo/MyItemPresenterDemo.xaml
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.BaseControl.ContentControlDemo
{
public sealed partial class ContentPresenterDemo : Page
{
public ContentPresenterDemo()
{
this.InitializeComponent();
}
}
}

2、演示 ContentControl 的基础知识
Controls/BaseControl/ContentControlDemo/ContentControlDemo.xaml

<Page
x:Class="Windows10.Controls.BaseControl.ContentControlDemo.ContentControlDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.ContentControlDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <Page.Resources>
<!--
DataTemplate - 数据模板(其是 xaml 语言使用的一种方案,无法在 c# 中定义)
-->
<DataTemplate x:DataType="common:Employee" x:Key="DataTemplateMale">
<Grid Background="Blue">
<TextBlock Text="{x:Bind Name}" />
</Grid>
</DataTemplate>
<DataTemplate x:DataType="common:Employee" x:Key="DataTemplateFemale">
<Grid Background="Pink">
<TextBlock Text="{x:Bind Name}" />
</Grid>
</DataTemplate> <!--
自定义数据模板选择器(参见 code-behind 中的代码)
-->
<local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
DataTemplate1="{StaticResource DataTemplateMale}"
DataTemplate2="{StaticResource DataTemplateFemale}" />
</Page.Resources> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <Button Name="button" Content="换个人" Click="button_Click" Margin="5" /> <ContentControl Name="contentControl" Width="400" Margin="5" HorizontalAlignment="Left"
ContentTemplateSelector="{StaticResource MyDataTemplateSelector}">
<ContentControl.ContentTransitions>
<TransitionCollection>
<ContentThemeTransition/>
</TransitionCollection>
</ContentControl.ContentTransitions>
</ContentControl> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/ContentControlDemo/ContentControlDemo.xaml.cs

/*
* ContentControl - ContentControl(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
* Content - 呈现的内容
* ContentTemplate - 数据模板(DataTemplate)
* ContentTemplateSelector - 数据模板选择器(如果指定了 ContentTemplate 则此配置无效)
* ContentTemplateRoot - 用于获取当前数据模板的根元素(写自定义 ContentControl 时会用到)
* ContentTransitions - Content 发生变化时的过度效果
*/ using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Controls.BaseControl.ContentControlDemo
{
public sealed partial class ContentControlDemo : Page
{
private IList<Employee> Employees { get; set; } = TestData.GetEmployees(); public ContentControlDemo()
{
this.InitializeComponent();
} private void button_Click(object sender, RoutedEventArgs e)
{
// 注:
// 在 Content 发生变化时会触发 ContentTemplateSelector 和 ContentTransitions(如果只是 DataContext 发生变化则不会有此效果)
// 所以如果需要 ContentTemplateSelector 和 ContentTransitions 的话,则应该直接设置 ContentControl 的 Content 而不是 DataContext
contentControl.Content = Employees[new Random().Next(, )];
}
} // 自定义 DataTemplateSelector(数据模板选择器)
// 可以实现在 runtime 时,根据 item 的不同选择不同的数据模板
public class MyDataTemplateSelector : DataTemplateSelector
{
// 数据模板 1(配置在 xaml 端)
public DataTemplate DataTemplate1 { get; set; } // 数据模板 2(配置在 xaml 端)
public DataTemplate DataTemplate2 { get; set; } // 根据 item 的数据的不同,指定的不同的模板
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
var employee = item as Employee;
if (employee == null || employee.IsMale)
return DataTemplate1; // 男员工用数据模板 1
return DataTemplate2; // 女员工用数据模板 2 // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (DataTemplate)Application.Current.Resources["DataTemplateMale"];
}
}
}

3、演示 UserControl 的基础知识
Controls/BaseControl/UserControlDemo/UserControlDemo.xaml

<Page
x:Class="Windows10.Controls.BaseControl.UserControlDemo.UserControlDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.UserControlDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <UserControl Margin="10 0 10 10" HorizontalAlignment="Left" VerticalAlignment="Top">
<UserControl.Content>
<Rectangle Width="300" Height="100" Fill="Orange" />
</UserControl.Content>
</UserControl> <!--
UserControl 有一个 [ContentProperty(Name = "Content")] 声明,所以在写 xaml 的时候可以省略掉 UserControl.Content
-->
<UserControl Margin="10 130 10 10" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle Width="300" Height="100" Fill="Orange" />
</UserControl> </Grid>
</Page>

Controls/BaseControl/UserControlDemo/UserControlDemo.xaml.cs

/*
* UserControl - UserControl(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
* Content - 呈现的内容
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.BaseControl.UserControlDemo
{
public sealed partial class UserControlDemo : Page
{
public UserControlDemo()
{
this.InitializeComponent();
}
}
}

4、演示 Page 的基础知识
Controls/BaseControl/PageDemo/PageDemo.xaml

<Page
x:Class="Windows10.Controls.BaseControl.PageDemo.PageDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.PageDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" TextWrapping="Wrap" /> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/PageDemo/PageDemo.xaml.cs

/*
* Page - Page(继承自 UserControl, 请参见 /Controls/BaseControl/UserControlDemo/)
* TopAppBar, BottomAppBar - 参见 /Controls/NavigationControl/AppBarDemo.xaml 和 /Controls/NavigationControl/CommandBarDemo.xaml
* NavigationCacheMode, OnNavigatedFrom(), OnNavigatingFrom(), OnNavigatingFrom() - 参见 /Controls/NavigationControl/FrameDemo.xaml
* Frame - 获取当前 Page 的所属 Frame
*/ using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Controls.BaseControl.PageDemo
{
public sealed partial class PageDemo : Page
{
public PageDemo()
{
this.InitializeComponent(); this.Loading += page_Loading;
this.Loaded += page_Loaded;
this.Unloaded += page_Unloaded;
} // 在 OnNavigatedTo 之后,由外到内触发 Loading 事件,由内到外触发 Loaded 事件;在 OnNavigatedFrom 之后,由外到内触发 Unloaded 事件(参见 /Controls/BaseControl/FrameworkElementDemo/Demo2.xaml.cs)
protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text += "OnNavigatedTo";
lblMsg.Text += Environment.NewLine;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Debug.WriteLine("OnNavigatingFrom");
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedFrom");
} // 在 OnNavigatedTo 之后,由外到内触发 Loading 事件,由内到外触发 Loaded 事件;在 OnNavigatedFrom 之后,由外到内触发 Unloaded 事件(参见 /Controls/BaseControl/FrameworkElementDemo/Demo2.xaml.cs)
private void page_Loading(FrameworkElement sender, object args)
{
lblMsg.Text += "page_Loading";
lblMsg.Text += Environment.NewLine;
}
private void page_Loaded(object sender, RoutedEventArgs e)
{
lblMsg.Text += "page_Loaded";
lblMsg.Text += Environment.NewLine;
}
private void page_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("page_Unloaded");
}
}
}

OK
[源码下载]

背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page的更多相关文章

  1. 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画)

    [源码下载] 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画) 作者:webabcd 介绍背水一战 Windows 10 之 动画 PopInThemeA ...

  2. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  3. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  4. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  5. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  6. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  7. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  8. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  9. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

随机推荐

  1. myeclipse和jdk的安装和配置

    一.安装JDK 1.下载得到jdk-8u11-windows-i586.1406279697.exe,直接双击运行安装,一直next就可以,默认是安装到系统盘下面Program Files,我这里装在 ...

  2. 001之IP基础对话框

    在TCP/IP协议中,建立连接的两个进程(客户端和服务器)各自用一个socket(IP地址+TCP/UDP端口号)标识.在MFC中流式套接字(SOCK_STREAM)和数据报套接字(SOCK_DGRA ...

  3. 一个RTSP/RTP over TCP 的丢包引起的问题

    背景知识:可以查看https://www.cnblogs.com/lidabo/p/4483497.html RTSP/RTP over TCP TCP承载RTSP/RTP   When you us ...

  4. Python3 复制和深浅copy

    赋值: 列表的赋值: list1 = ['peter','sam'] list2 = list1 print(list1,id(list1)) print(list2,id(list2)) list1 ...

  5. 面试加分项---HashMap底层实现原理

    想必大家都知道HashSet和HashMap之间的关系,HashSet是依赖于HashMap的,HashSet集合就是HashMap的key所组成的集合,我们都知道HashMap的value是可以重复 ...

  6. 性感天才黑客乔治·霍兹George Hotz 17岁打脸乔布斯20岁搞疯索尼

    1.国内外著名黑客信息 1) 国外著名黑客 George Hotz 乔治·霍兹(George Hotz,1989年10月2日-),美国学生,2007年8月解锁苹果(Apple)iPhone手机,使得i ...

  7. jeesite 下载ckfinder上传的文件

    在需要下载的位置,将以下代码复制到页面最下方,就可以实现文件下载了 <script> $(document).ready(function() { var fileName = $(&qu ...

  8. layui-tree创建下拉树型选项框

    1.简介 也许你对树菜单再熟悉不过了,然而你仍有可能更青睐layui的tree,它也许比以往你知道的任何一个类似的组件都更轻量.简单,你在享受许多功能的同时,甚至不用去记太多的参数. 另外,最大的重点 ...

  9. 【Django】HTTP status code must be an integer.

    刚刚出现这个问题,还以为是表单提交或者什么网络错误 结果发现是自己的低级错误写了 HttpResponse(request,'sigin_result2.html',context)这个根本不能渲染模 ...

  10. python命名规则

    1 包.模块的命名规则:全部以小写字母形式来命名.比如:import random 2 类.对象的命名规则:类是每个单词的首字母要大写,其他字母小写比如:class MyFamily: ,类的私有属性 ...