WPF控件内容模型

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>
效果如下:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值