[源码下载]

背水一战 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. python使用细节

    python的函数位置参数在调用时可以直接传参,也可以a=5,b=7的形式传参,原以为kw参数才可以. >>> def f(a,b): print a+b >>> ...

  2. Spring MVC和Spring Boot的理解以及比较

    Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...

  3. javascript中let和var的区别

    let是es6中新增命令,也是用来声明变量的,可能很多小伙伴都像我一样,定义变量的时候都会用var而很少用到let,那么,let和var到底有什么区别呢? let和var的区别体现在作用域上.var的 ...

  4. leetcode49

    public class Solution { public IList<IList<string>> GroupAnagrams(string[] strs) { var L ...

  5. Spring事务隔离级别和传播性

    事务的隔离级别也分为四种: read uncommited(读未提交). read commited(读提交). read repeatable(读重复). serializable(序列化), 这四 ...

  6. autolayout后获取frame

    autolayout设置完layout立即用frame拿对应的值可能拿不准,因为autolayout设置完布局后布局引擎并不会马上去更改布局,而是将布局标记为待更新,此时可以用的方法有两种,一是延时0 ...

  7. PAT甲级

    https://www.cnblogs.com/jlyg/p/7525244.html 哇咔咔,瞧人家都刷完甲级130道题了,哼,有什么了不起的,考前刷完,再刷的比你多,哼,照样吊打. 沙耶加油! 一 ...

  8. iptables随笔

    iptables 分为四表五链 四表: filter表 nat 表 mangle 表 raw 表 五链 INPUT 链 OUTPUT 链 FORWARD 链 PREROUTING(路由前) POSTR ...

  9. Asp.net之Sql注入与Parameter对象

    一.Sql注入实例 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlCl ...

  10. Modelsim command line 传参数到 .do 文件

    gui跑mdelsim总觉得很麻烦,使用命令来启动方便了很多,类似linux一样,其实目前windows也可以做到,只是业界不怎么用windows罢了. 基于modelsim搭了一个UVM环境,  用 ...