WPF-15 内容模型

WPF是一个客户端数据展示框架,框架提供了许多控件,来显示不同的内容,我们在WPF-13 控件分类中介绍到基本控件分类以及派生体系,这节我们主要介绍控件内容模型,所谓内容模型就是控件中可以显示哪些内容,每个内容模型都有内容属性.
包含一个任意对象的控件

继承自ContentControl类型的控件可以包含任意内容,它的Content属性,是一个Object类型,典型的控件有(Button、ButtonBase、CheckBox、ComboBoxItem、ContentControl、Frame、GridViewColumnHeader、GroupItem、Label、ListBoxItem、ListViewItem、NavigationWindow、RadioButton、RepeatButton、ScrollViewer、StatusBarItem、ToggleButton、ToolTip、UserControl、Window)下面Content设置为字符串、DateTime 对象、Rectangle 和包含 Ellipse 

<Window x:Class="Example_13.MainWindow"        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:Example_13"        xmlns:sys="clr-namespace:System;assembly=mscorlib"        mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">    <Grid>        <Grid.RowDefinitions>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>            <RowDefinition></RowDefinition>        </Grid.RowDefinitions>        <Button Grid.Row="0" Margin="10,10,10,10">            <Button.Content>                <sys:String>包含一个字符串内容的Button</sys:String>            </Button.Content>        </Button>        <Button Grid.Row="1"  Margin="10,10,10,10">            <Button.Content>                <sys:DateTime>2004/3/4 13:6:55</sys:DateTime>            </Button.Content>        </Button>        <Button Grid.Row="2"  Margin="10,10,10,10">            <Button.Content>                <Rectangle Fill="Red"  Width="100" Height="100">                </Rectangle>            </Button.Content>        </Button>        <Button Grid.Row="3"  Margin="10,10,10,10">            <Button.Content>                <Ellipse Fill="Green"  Width="100" Height="100">                </Ellipse>            </Button.Content>        </Button>    </Grid></Window>

WPF-15 内容模型

包含一个标头和一个任意对象的控件

HeaderedContentControl 类继承自ContentControl, 包含标题的内容和Content属性(继承自ContentControl),并定义Object 类型的 Header,具有代表性控件(Expander、GroupBox、TabItem)
下图显示了一个Expander和GroupBox对象,Expander控件中Header包含了一个String对象,Content包含TextBlock的对象。在GroupBox控件中,第一个 TabItem具有作为 Header 和 Content 的 UIElement 对象。Header设置为包含 Ellipse 和 TextBlock 的 StackPanel,Content 包含TextBlock 和TextBox 的 StackPanel,第二个 TabItem 在 Header 中包含字符串,并在 Content 中包含 TextBlock
<Window x:Class="Example_13.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:Example_13" mc:Ignorable="d" Title="Window1" Height="450" Width="800">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition></ColumnDefinition>            <ColumnDefinition></ColumnDefinition>        </Grid.ColumnDefinitions>        <Expander Grid.Column="0" Name="myExpander" Background="Tan"           ExpandDirection="Down"  IsExpanded="True">            <Expander.Header>                Expander Control            </Expander.Header>            <TextBlock TextWrapping="Wrap">                Expander 控件的Content属性            </TextBlock>        </Expander>        <GroupBox Grid.Column="1" Width="300" Height="410">            <GroupBox.Header>                <Label>Tab Control</Label>            </GroupBox.Header>            <StackPanel>                <TabControl Name="myTabControl" TabStripPlacement="Top" Margin="0, 0, 0, 10" Height="350">                    <TabItem>                        <TabItem.Header>                            <StackPanel Orientation="Horizontal">                                <Ellipse Fill="Gray"  Width="10" Height="10"></Ellipse>                                <TextBlock Margin="5,0,0,0">Tab 1</TextBlock>                            </StackPanel>                        </TabItem.Header>                        <StackPanel Orientation="Vertical">                            <TextBlock>输入信息:</TextBlock>                            <TextBox></TextBox>                        </StackPanel>                    </TabItem>                    <TabItem>                        <TabItem.Header>Tab 2</TabItem.Header>                        <TextBlock>文本信息</TextBlock>                    </TabItem>                </TabControl>            </StackPanel>        </GroupBox>    </Grid></Window>

WPF-15 内容模型

包含集合(集合可以是任意对象)的控件

ItemsControl类继承自Control,可以包含多个项,例如字符串、对象或其他元素。它的内容属性为ItemsSource和Items。ItemsSource 通常用于使用数据集合填充ItemsControl,如果不想使用集合填充 ItemsControl,可使用Items属性添加项。典型控件有(Menu、MenuBase、ContextMenu、ComboBox、ItemsControl、ListBox、ListView、TabControl、TreeView、Selector、StatusBar)我们可以通过下图可以看到Items属性返回ItemCollection和ItemsSource返回IEnumerable的对象

WPF-15 内容模型

WPF-15 内容模型

下图显示了包含这些类型的项的 ListBox:一个字符串、DateTime 对象、一个 UIElement、Panel包含一个 Ellipse 和一个 TextBlock

<Window x:Class="Example_13.Window2"        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:Example_13"        xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="Window2" Height="450" Width="800">    <Grid>        <ListBox>            <ListBoxItem>                字符创类型            </ListBoxItem>            <ListBoxItem>                <sys:DateTime>2004/3/4 13:6:55</sys:DateTime>            </ListBoxItem>            <ListBoxItem>                <Rectangle Fill="Red"  Width="40" Height="40">                </Rectangle>            </ListBoxItem>            <ListBoxItem>                <StackPanel Orientation="Horizontal">                    <Ellipse Fill="Green" Width="30" Height="30"></Ellipse>                    <TextBlock> TextBlock</TextBlock>                </StackPanel>            </ListBoxItem>        </ListBox>    </Grid></Window>

WPF-15 内容模型

包含一个标头和对象集合的控件

HeaderedItemsControl类继承自ItemsControl,可以包含多个项,例如字符串、对象或其他元素,也可以包含标题。它继承ItemsControl内容属性 ItemsSource 和 Items,并定义可以是任意对象的 Header 属性。典型的控件有(MenuItem、ToolBar、TreeViewItem)

包含一个UIElement对象集合的类
Panel 类定位和排列 UIElement 子对象。它的内容属性为 Children。典型的控件有(Canvas、DockPanel、Grid、TabPanel、ToolBarOverflowPanel、ToolBarPanel、UniformGrid、StackPanel、VirtualizingPanel、VirtualizingStackPanel、WrapPanel)
UIElement元素外观类
Decorator类将视觉效果应用到单个UIElement子对象上或应用到其周围,典型的控件有(AdornerDecorator、Border、BulletDecorator、ButtonChrome、ClassicBorderDecorator、InkPresenter、ListBoxChrome、SystemDropShadowChrome、Viewbox)
 
我们这篇主要WPF中常用的4大类内容模型控件:ContentControl、HeaderedContentControl、ItemsControl、HeaderedItemsControl

WPF-15 内容模型

另外还介绍了可以排版UIElement控件的Panel控件以及UIElement装饰控件,希望对各位在学习WPF的道理上有所帮助。