在我們平時的項目中,我們經常需要一套自己的自定義控件庫,這個特別是在Prism這種框架下面進行開發的時候,每個人都使用一套統一的控件,這樣才不會每個人由於界面不統一而造成的整個軟件系統千差萬別,所以我們需要創建自己的一套界面庫。下面介紹如何在WPF程序下創建自定義控件庫。
1 在我們的項目中,在解決方案右鍵-》新建項目,添加“WPF自定義控件庫”。
2 在默認生成的項目下面,會有兩個文件,一個是“Generic.xaml”文件,另外一個是繼承自Control類的CustomControl1這個文件,默認情況下這個文件的內容為:
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
在這個類里面,我們可以定義需要綁定到前台的各種依賴項屬性,從而在后台實現數據的綁定。
3 我們有時需要添加資源字典xaml文件,但是添加的資源字典必須放在Generic.xaml文件下面,比如我們添加了一個Group.xaml的文件,最后在Generic.xaml文件下必須通過ResourceDictionary.MergedDictionaries的方式將Group.xaml放在里面,這個是必須要引起注意的地方,否則添加的自定義控件是不能夠進行識別的。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/X.UI;component/Themes/Group.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
4 另外在定義完了Group.xaml文件之后,我們需要定義對應的Group.cs文件從而和前台的Group.xaml文件進行交互,這里我們需要定義很多依賴項屬性。
5 編譯生成DLL文件。
6 在其他項目中通過引用該程序集,通過clr-namespace和assembly引用該程序集,並達到引用該控件的目的。
下面貼出Group.xaml和Group.cs文件來說明如何創建一個簡單的自定義控件庫。
Group.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:X.UI.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/X.UI.Controls;Component/Themes/Public.xaml"></ResourceDictionary>
<ResourceDictionary Source="/X.UI.Controls;Component/Themes/YouDaoPing.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="local:Group">
<Setter Property="Background" Value="#eee"></Setter>
<Setter Property="HeaderBackground" Value="{StaticResource Decorative}"></Setter>
<Setter Property="Foreground" Value="{StaticResource DarkColor}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:Group">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="{TemplateBinding HeaderBackground}" Padding="10 0">
<Grid>
<ContentPresenter Content="{TemplateBinding Header}" HorizontalAlignment="{TemplateBinding HeaderAlign}" VerticalAlignment="Center"></ContentPresenter>
<ContentPresenter Content="{TemplateBinding ToolBar}" HorizontalAlignment="{TemplateBinding ToolBarAlign}" VerticalAlignment="Center"></ContentPresenter>
</Grid>
</Border>
<Border Background="{TemplateBinding Background}" Grid.Row="1">
<ContentPresenter Margin="{TemplateBinding Padding}"></ContentPresenter>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Group.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
namespace X.UI.Controls
{
/// <summary>
/// 按照步驟 1a 或 1b 操作,然后執行步驟 2 以在 XAML 文件中使用此自定義控件。
///
/// 步驟 1a) 在當前項目中存在的 XAML 文件中使用該自定義控件。
/// 將此 XmlNamespace 特性添加到要使用該特性的標記文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:X.UI.Controls"
///
///
/// 步驟 1b) 在其他項目中存在的 XAML 文件中使用該自定義控件。
/// 將此 XmlNamespace 特性添加到要使用該特性的標記文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:X.UI.Controls;assembly=X.UI.Controls"
///
/// 您還需要添加一個從 XAML 文件所在的項目到此項目的項目引用,
/// 並重新生成以避免編譯錯誤:
///
/// 在解決方案資源管理器中右擊目標項目,然后依次單擊
/// “添加引用”->“項目”->[選擇此項目]
///
///
/// 步驟 2)
/// 繼續操作並在 XAML 文件中使用控件。
///
/// <MyNamespace:CustomControl1/>
///
/// </summary>
public class Group : System.Windows.Controls.HeaderedContentControl
{
static Group()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Group), new FrameworkPropertyMetadata(typeof(Group)));
}
public HorizontalAlignment HeaderAlign
{
get
{
return (HorizontalAlignment)GetValue(HeaderAlignProperty);
}
set
{
SetValue(HeaderAlignProperty, value);
}
}
// Using a DependencyProperty as the backing store for HeaderAlign. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderAlignProperty =
DependencyProperty.Register("HeaderAlign", typeof(HorizontalAlignment), typeof(Group), new PropertyMetadata(HorizontalAlignment.Center));
public Panel ToolBar
{
get
{
return (Panel)GetValue(ToolBarProperty);
}
set
{
SetValue(ToolBarProperty, value);
}
}
// Using a DependencyProperty as the backing store for ToolBar. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ToolBarProperty =
DependencyProperty.Register("ToolBar", typeof(Panel), typeof(Group), new PropertyMetadata(null));
public HorizontalAlignment ToolBarAlign
{
get
{
return (HorizontalAlignment)GetValue(ToolBarAlignProperty);
}
set
{
SetValue(ToolBarAlignProperty, value);
}
}
public static readonly DependencyProperty ToolBarAlignProperty =
DependencyProperty.Register("ToolBarAlign", typeof(HorizontalAlignment), typeof(Group), new PropertyMetadata(HorizontalAlignment.Right));
public Brush HeaderBackground
{
get
{
return (Brush)GetValue(HeaderBackgroundProperty);
}
set
{
SetValue(HeaderBackgroundProperty, value);
}
}
// Using a DependencyProperty as the backing store for HeaderBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderBackgroundProperty =
DependencyProperty.Register("HeaderBackground", typeof(Brush), typeof(Group), new PropertyMetadata(null));
}
}
在這里我們也可以着重看一下該文件中注釋的部分。
