一、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;
}
}
}
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;
}
}
}