[源码下载]

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

作者:webabcd

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

  • ContentPresenter
  • ContentControl
  • UserControl
  • Page

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

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.ContentControlDemo.ContentPresenterDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.ContentControlDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <ContentControl Width="400" Margin="5" Content="我是 ContentControl" HorizontalAlignment="Left">
  14. <ContentControl.Template>
  15. <ControlTemplate>
  16. <Border BorderBrush="Red" BorderThickness="1">
  17. <Grid Background="Yellow">
  18. <!--
  19. ContentPresenter - 用来呈现 ContentControl 的 Content
  20. 有一堆属性,相关说明请参见文档
  21. -->
  22. <ContentPresenter HorizontalAlignment="Right" Foreground="Black" FontSize="24" Padding="20" />
  23. </Grid>
  24. </Border>
  25. </ControlTemplate>
  26. </ContentControl.Template>
  27. </ContentControl>
  28.  
  29. </StackPanel>
  30. </Grid>
  31. </Page>

Controls/BaseControl/ContentControlDemo/ContentPresenterDemo.xaml.cs

  1. /*
  2. * ContentPresenter - 用来呈现 ContentControl 的 Content(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
  3. *
  4. *
  5. * 注:关于如何创建自定义的 ContentPresenter 请参见 /Controls/CollectionControl/ItemsControlDemo/MyItemPresenterDemo.xaml
  6. */
  7.  
  8. using Windows.UI.Xaml.Controls;
  9.  
  10. namespace Windows10.Controls.BaseControl.ContentControlDemo
  11. {
  12. public sealed partial class ContentPresenterDemo : Page
  13. {
  14. public ContentPresenterDemo()
  15. {
  16. this.InitializeComponent();
  17. }
  18. }
  19. }

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

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.ContentControlDemo.ContentControlDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.ContentControlDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9.  
  10. xmlns:common="using:Windows10.Common">
  11.  
  12. <Page.Resources>
  13. <!--
  14. DataTemplate - 数据模板(其是 xaml 语言使用的一种方案,无法在 c# 中定义)
  15. -->
  16. <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateMale">
  17. <Grid Background="Blue">
  18. <TextBlock Text="{x:Bind Name}" />
  19. </Grid>
  20. </DataTemplate>
  21. <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateFemale">
  22. <Grid Background="Pink">
  23. <TextBlock Text="{x:Bind Name}" />
  24. </Grid>
  25. </DataTemplate>
  26.  
  27. <!--
  28. 自定义数据模板选择器(参见 code-behind 中的代码)
  29. -->
  30. <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
  31. DataTemplate1="{StaticResource DataTemplateMale}"
  32. DataTemplate2="{StaticResource DataTemplateFemale}" />
  33. </Page.Resources>
  34.  
  35. <Grid Background="Transparent">
  36. <StackPanel Margin="10 0 10 10">
  37.  
  38. <Button Name="button" Content="换个人" Click="button_Click" Margin="5" />
  39.  
  40. <ContentControl Name="contentControl" Width="400" Margin="5" HorizontalAlignment="Left"
  41. ContentTemplateSelector="{StaticResource MyDataTemplateSelector}">
  42. <ContentControl.ContentTransitions>
  43. <TransitionCollection>
  44. <ContentThemeTransition/>
  45. </TransitionCollection>
  46. </ContentControl.ContentTransitions>
  47. </ContentControl>
  48.  
  49. </StackPanel>
  50. </Grid>
  51. </Page>

Controls/BaseControl/ContentControlDemo/ContentControlDemo.xaml.cs

  1. /*
  2. * ContentControl - ContentControl(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
  3. * Content - 呈现的内容
  4. * ContentTemplate - 数据模板(DataTemplate)
  5. * ContentTemplateSelector - 数据模板选择器(如果指定了 ContentTemplate 则此配置无效)
  6. * ContentTemplateRoot - 用于获取当前数据模板的根元素(写自定义 ContentControl 时会用到)
  7. * ContentTransitions - Content 发生变化时的过度效果
  8. */
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using Windows.UI.Xaml;
  13. using Windows.UI.Xaml.Controls;
  14. using Windows10.Common;
  15.  
  16. namespace Windows10.Controls.BaseControl.ContentControlDemo
  17. {
  18. public sealed partial class ContentControlDemo : Page
  19. {
  20. private IList<Employee> Employees { get; set; } = TestData.GetEmployees();
  21.  
  22. public ContentControlDemo()
  23. {
  24. this.InitializeComponent();
  25. }
  26.  
  27. private void button_Click(object sender, RoutedEventArgs e)
  28. {
  29. // 注:
  30. // 在 Content 发生变化时会触发 ContentTemplateSelector 和 ContentTransitions(如果只是 DataContext 发生变化则不会有此效果)
  31. // 所以如果需要 ContentTemplateSelector 和 ContentTransitions 的话,则应该直接设置 ContentControl 的 Content 而不是 DataContext
  32. contentControl.Content = Employees[new Random().Next(, )];
  33. }
  34. }
  35.  
  36. // 自定义 DataTemplateSelector(数据模板选择器)
  37. // 可以实现在 runtime 时,根据 item 的不同选择不同的数据模板
  38. public class MyDataTemplateSelector : DataTemplateSelector
  39. {
  40. // 数据模板 1(配置在 xaml 端)
  41. public DataTemplate DataTemplate1 { get; set; }
  42.  
  43. // 数据模板 2(配置在 xaml 端)
  44. public DataTemplate DataTemplate2 { get; set; }
  45.  
  46. // 根据 item 的数据的不同,指定的不同的模板
  47. protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
  48. {
  49. var employee = item as Employee;
  50. if (employee == null || employee.IsMale)
  51. return DataTemplate1; // 男员工用数据模板 1
  52. return DataTemplate2; // 女员工用数据模板 2
  53.  
  54. // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (DataTemplate)Application.Current.Resources["DataTemplateMale"];
  55. }
  56. }
  57. }

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

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.UserControlDemo.UserControlDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.UserControlDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <UserControl Margin="10 0 10 10" HorizontalAlignment="Left" VerticalAlignment="Top">
  13. <UserControl.Content>
  14. <Rectangle Width="300" Height="100" Fill="Orange" />
  15. </UserControl.Content>
  16. </UserControl>
  17.  
  18. <!--
  19. UserControl 有一个 [ContentProperty(Name = "Content")] 声明,所以在写 xaml 的时候可以省略掉 UserControl.Content
  20. -->
  21. <UserControl Margin="10 130 10 10" HorizontalAlignment="Left" VerticalAlignment="Top">
  22. <Rectangle Width="300" Height="100" Fill="Orange" />
  23. </UserControl>
  24.  
  25. </Grid>
  26. </Page>

Controls/BaseControl/UserControlDemo/UserControlDemo.xaml.cs

  1. /*
  2. * UserControl - UserControl(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
  3. * Content - 呈现的内容
  4. */
  5.  
  6. using Windows.UI.Xaml.Controls;
  7.  
  8. namespace Windows10.Controls.BaseControl.UserControlDemo
  9. {
  10. public sealed partial class UserControlDemo : Page
  11. {
  12. public UserControlDemo()
  13. {
  14. this.InitializeComponent();
  15. }
  16. }
  17. }

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

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.PageDemo.PageDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.PageDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock Name="lblMsg" Margin="5" TextWrapping="Wrap" />
  14.  
  15. </StackPanel>
  16. </Grid>
  17. </Page>

Controls/BaseControl/PageDemo/PageDemo.xaml.cs

  1. /*
  2. * Page - Page(继承自 UserControl, 请参见 /Controls/BaseControl/UserControlDemo/)
  3. * TopAppBar, BottomAppBar - 参见 /Controls/NavigationControl/AppBarDemo.xaml 和 /Controls/NavigationControl/CommandBarDemo.xaml
  4. * NavigationCacheMode, OnNavigatedFrom(), OnNavigatingFrom(), OnNavigatingFrom() - 参见 /Controls/NavigationControl/FrameDemo.xaml
  5. * Frame - 获取当前 Page 的所属 Frame
  6. */
  7.  
  8. using System;
  9. using System.Diagnostics;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Navigation;
  13.  
  14. namespace Windows10.Controls.BaseControl.PageDemo
  15. {
  16. public sealed partial class PageDemo : Page
  17. {
  18. public PageDemo()
  19. {
  20. this.InitializeComponent();
  21.  
  22. this.Loading += page_Loading;
  23. this.Loaded += page_Loaded;
  24. this.Unloaded += page_Unloaded;
  25. }
  26.  
  27. // 在 OnNavigatedTo 之后,由外到内触发 Loading 事件,由内到外触发 Loaded 事件;在 OnNavigatedFrom 之后,由外到内触发 Unloaded 事件(参见 /Controls/BaseControl/FrameworkElementDemo/Demo2.xaml.cs)
  28. protected override void OnNavigatedTo(NavigationEventArgs e)
  29. {
  30. lblMsg.Text += "OnNavigatedTo";
  31. lblMsg.Text += Environment.NewLine;
  32. }
  33. protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
  34. {
  35. Debug.WriteLine("OnNavigatingFrom");
  36. }
  37. protected override void OnNavigatedFrom(NavigationEventArgs e)
  38. {
  39. Debug.WriteLine("OnNavigatedFrom");
  40. }
  41.  
  42. // 在 OnNavigatedTo 之后,由外到内触发 Loading 事件,由内到外触发 Loaded 事件;在 OnNavigatedFrom 之后,由外到内触发 Unloaded 事件(参见 /Controls/BaseControl/FrameworkElementDemo/Demo2.xaml.cs)
  43. private void page_Loading(FrameworkElement sender, object args)
  44. {
  45. lblMsg.Text += "page_Loading";
  46. lblMsg.Text += Environment.NewLine;
  47. }
  48. private void page_Loaded(object sender, RoutedEventArgs e)
  49. {
  50. lblMsg.Text += "page_Loaded";
  51. lblMsg.Text += Environment.NewLine;
  52. }
  53. private void page_Unloaded(object sender, RoutedEventArgs e)
  54. {
  55. Debug.WriteLine("page_Unloaded");
  56. }
  57. }
  58. }

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. 0初识Linux

    今天三八妇女节,Linux就该这么学,开课第一天.信心满满,激动,期待,要努力了.(博客为预习写的,今天又做了更新.)   Linux第一印象就是黑色背景屏幕,上面还有好多代码,敲的一手好的命令操控着 ...

  2. mvc中view与controll之间传递参数时,可以使用url进行传递

    mvc中view与controller之间传递参数时,可以使用url进行传递,但是在url的地址中需要加上“id=123”这样的东西才行. 具体如代码: window.location.href = ...

  3. 【转】vMAN 和 PVID

    vMAN关的情况下,如果用户的包内带有VLAN TAG,则以用户的TAG为准,如果用户的包内不带VLAN TAG,就打上PVID:vMAN开的情况下,无论用户的包内是否带有VLAN TAG,都强制在外 ...

  4. 168. Excel Sheet Column Title (Math)

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  5. oracle 新增并返回新增的主键

    oracle 的insert into 语句需要返回新增的主键的时候,可以使用一下insert 语法: insert into ims.t_bank_inquire_results (t_date,l ...

  6. [SpringBoot]Web综合开发-笔记

    Web开发 Json接口开发 @RestController 给类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回. 自定义filter filter作用: 用于 ...

  7. Scrapy反爬

    1,随机更换 user-agent: 将足够多的user-agent放在settings中,在parse方法中调用 缺点:每一个request中都要调用这个方法 这个是scrapy的流程图. 既然每一 ...

  8. vue-router 动态添加 路由

    动态添加路由可以用了做权限管理.登录后服务器端返回权限菜单,前端动态添加路由  然后在设置菜单 1.vue-router 有方法router.addRoutes(routes) 动态添加更多的路由规则 ...

  9. PHP多进程实例

    PHP创建多进程需要使用到pcntl模块 在编译时加上--enable-pcntl打开进程控制支持,不是Unix类系统不支持此模块 php官网介绍http://php.net/manual/zh/bo ...

  10. Python开发——数据结构【深浅拷贝】

    浅拷贝 # 浅拷贝只copy一层 s = [3,'Lucy',4,[1,2]] s1 = s.copy() 深拷贝 # 深拷贝——克隆一分 import copy s = [3,'Lucy',4,[1 ...