WPF控件内容模型主要指派生于System.Windows.Controls.Control類的各種控件,其主要分為四部分:
•ContentControl
•HeaderedContendControl
•ItemsControl
•HeaderedItemsControl
這四個類用作為 WPF 中大多數控件的基類。使用這些内容模型的類可以包含相同類型的内容,并以相同的方式處理該内容;可以放置在某個 ContentControl(或從 ContentControl 繼承的類)中的任何類型的對象都可以放置在具有其他三個内容模型中的任何一個的控件中。如:
<Window x:Class="WpfApplication1.控件内容模闆"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="控件内容模闆" Height="300" Width="300">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--ContentControl示例(Button)-->
<TextBlock Text="ContentControl_Test" Grid.Column="0" Grid.Row="0"/>
<Button Margin="0,25,5,10" Grid.Column="0" Grid.Row="0">
<Button.Content>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Image Source="image/1.jpg" Width="100" Height="80"></Image>
<TextBlock Text="美麗的花兒" HorizontalAlignment="Center"></TextBlock>
</StackPanel>
</Button.Content>
</Button>
<!--HeaderedContentControl示例(GroupBox)-->
<TextBlock Text="HeaderedContentControl" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center"/>
<GroupBox Margin="5,20,5,5" Grid.Row="0" Grid.Column="1">
<GroupBox.Header>
<TextBlock Text="Header Text"/>
</GroupBox.Header>
<GroupBox.Content>
<StackPanel>
<Image Source="image/2.jpg" Height="60"></Image>
<TextBlock Text="小圖檔2" HorizontalAlignment="Center"/>
</StackPanel>
</GroupBox.Content>
</GroupBox>
<!--ItemsControl示例(ListBox)-->
<TextBlock Text="ItemsControl" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center"/>
<ListBox Margin="5,20,5,5" Grid.Row="1" Grid.Column="0">
<ListBox.Items>
<TextBlock Text="List A Item"/>
<Button Content="List B item" Click="Button_Click"/>
<StackPanel>
<Image Source="image/3.jpg" Height="50"/>
<TextBlock HorizontalAlignment="Center">小圖檔3</TextBlock>
</StackPanel>
</ListBox.Items>
</ListBox>
<!--HeaderedItemsControl示例(TreeView)-->
<TextBlock Text="HeaderedItemsControl" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center"/>
<TreeView Margin="5,20,5,5" Grid.Row="1" Grid.Column="1">
<TreeViewItem>
<TreeViewItem.Header> Tree Root Node A </TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="Node A - 1" />
<Button Content="Node A - 2" />
<StackPanel Orientation="Horizontal">
<Image Source="Image/4.jpg" Width="48" Height="48" />
<TextBlock Text="圖檔4" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</TreeViewItem.Items>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
Tree Node B
</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="Node B - 1" />
<Button Content="Node B - 2" Click="Button_Click" />
</TreeViewItem.Items>
</TreeViewItem>
</TreeView>
<StackPanel Grid.Column="0" Grid.Row="2">
<Button >
<!--Stretch="Fill" 表示填充滿格-->
<Image Source="image/5.jpg" Height="50"/>
</Button>
<Button Content="測試" Height="50"/>
</StackPanel>
</Grid>
</Window>
效果如下: