1. 新建一個wpf工程,在工程下面新建
一個文件夾themes,在themes下新建兩個資源字典文件generic.xaml和PrettySeekBar.xaml
generic.xaml
<
ResourceDictionary
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns
:
x
="http://schemas.microsoft.com/winfx/2006/xaml">
<
ResourceDictionary.MergedDictionaries
>
<
ResourceDictionary
Source
="/PrettyControls;component/themes/PrettySeekBar.xaml" />
</
ResourceDictionary.MergedDictionaries
>
</
ResourceDictionary
>
PrettySeekBar.xaml
<
ResourceDictionary
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns
:
x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns
:
Pretty
="clr-namespace:PrettyControls"
>
<
Style
TargetType
="{
x
:
Type
Pretty
:
PrettySeekBar
}">
<
Setter
Property
="Template">
<
Setter.Value
>
<
ControlTemplate
TargetType
="{
x
:
Type
Pretty
:
PrettySeekBar
}">
<
Grid
Width
="50"
Height
="50"
Background
="Red">
</
Grid
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
</
ResourceDictionary
>
2. 新建一個類PrettySeekBar
namespace
PrettyControls
{
public
class
PrettySeekBar
:
Control
{
#region
Constructors
static
PrettySeekBar()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof
(
PrettySeekBar
),
new
FrameworkPropertyMetadata
(
typeof
(
PrettySeekBar
)));
}
#endregion
}
}
3. 將wpf工程改成類庫工程,而且刪除 app.xaml 和 MainWindow.xaml以及對應的cs文件。
之因此新建一個wpf工程而不是直接新建類庫共,是由於wpf功能會自動導入wpf項目須要的基本類庫。
4. 新建一個Test wpf工程,而且引用PrettyControls項目,而後添加以下:
<
Window
x
:
Class
="Test.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns
:
x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="MainWindow"
Height
="350"
Width
="525"
xmlns
:
Pretty
="clr-namespace:PrettyControls;assembly=PrettyControls"
>
<
Grid
>
<
Pretty
:
PrettySeekBar
/>
</
Grid
>
</
Window
>
這樣就完成自定義控件的第一步了即,顯示一個方框。