现在已经会了通过blend,制作控件的style和模板了,但是,我今天提出了一个想法,我能否自己写一个像button那样的控件类呢?
然后我的应用程序直接继承我自己写的类,就可以直接像button类那样呢?说的通俗一点,举个例子,我想写个控件,但是需要设置一个image,暂用button的一半大小。但是原先的button类根本没有这个属性来给与赋值,我需要自定义一个属性。。这就是我要干的事情:
1,可以自定义样式
2,可以自定义控件的属性
通过几天的研究,有了一些眉目。首先我肯定还是需要使用blend来制作控件模板。然后我需要再定义一个类,再定义类的一些属性。然后我需要把这个类与我制作的控件模板绑定起来。。
这里我引入一个文件
Generic.xaml
这个文件的介绍可以参考下面
https://stackoverflow.com/questions/1228875/what-is-so-special-about-generic-xaml
另外,还需要再
AssemblyInfo.cs 文件中增加
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
后面就可以真正的实现关联啦。
下面开始编码
********************************************等待中************************************************
哈哈,第一个控件搞定了。可以睡觉了,困死。
上一篇使用blend写了个最小化窗口的图标,这次直接用来做控件了。把步骤记录一下
首先,新建一个控件类
public class FirstButton: Button
{
static FirstButton()
{
ElementBase.DefaultStyle<FirstButton>(DefaultStyleKeyProperty);
}
}
默认的构造函数,调用了一个加载默认样式的函数
public static void DefaultStyle<thisType>(DependencyProperty dp)
{
dp.OverrideMetadata(typeof(thisType), new FrameworkPropertyMetadata(typeof(thisType)));
}
然后新建一个xaml的文件,如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControl.Controls">
<Style TargetType="{x:Type local:FirstButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FirstButton}">
<Grid>
<Rectangle Name="rec" Fill="#FF01010E"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2">
<Rectangle Fill="#FFF9F9FC"/>
</Grid>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rec" Value="#5501010E"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
注意了,需要把<ControlTemplate TargetType="{x:Type local:FirstButton}"> 修改为我新建的类。
然后还需要修改Generic.xaml,把资源加载进来,看下面的代码,应该都可以看懂
这是Generic.xaml的内容
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControl">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyControl;component/Themes/Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
这是styles.xaml的内容
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyControl;component/Themes/Styles/FirstButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
大概意思就是需要把这个样式加载进来。
然后我又新建了一个wpf的工程,再看看我工具箱,是不是很神奇
哈哈,有啦,出来啦,出来啦,这就是我的第一个wpf的控件了。运行结果如下: