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