WPF内容控件详解

一、ContentControl模型
ContentControl模型的类型具有一个 Content 属性。Content 属性的类型为 Object,因此,对于您在 ContentControl 中可以放置的内容没有任何限制。可以使用可扩展应用程序标记语言 (XAML) 或代码来设置 Content。

以下控件使用 ContentControl 内容模型:

Button、ButtonBase、CheckBox、ComboBoxItem、ContentControl、Frame、GridViewColumnHeader、GroupItem、Label、ListBoxItem、ListViewItem、NavigationWindow、RadioButton、RepeatButton、ScrollViewer、StatusBarItem、ToggleButton、ToolTip、UserControl、Window

在Content中只能放置一个控件(可以放置一个容器,然后再在容器中放置多个控件)。

严格地说,Content的内容应该放置于<XXX.Content></XXX.Content>内部,但也可以省略此标记。如在按钮中放置一图片可以有以下几种写法:
<StackPanel Grid.Column="0" Grid.Row="2">
<Button >
<!--Stretch="Fill" 表示填充满格-->
<Image Source="image/5.jpg" Height="50"/>
</Button>
<Button Content="测试" Height="50"/>
</StackPanel>

另外,还可以使用代码来为ContentControl指定相应的Content属性,如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for 内容模板2.xaml
/// </summary>
public partial class 内容模板2 : Window
{
public 内容模板2()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBlock date = new TextBlock();
date.Text = DateTime.Now.ToString("yyyy-MM--dd");

TextBlock time = new TextBlock();
time.Text = DateTime.Now.ToString("hh:mm:ss");

StackPanel panel = new StackPanel();
panel.Children.Add(date);
panel.Children.Add(time);
// btn.Content = panel;
// this.AddChild(panel);
this.Content = panel;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值