Wpf 自定义控件(1)
2024-09-04 15:09:44
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>
这样就完成自定义控件的第一步了即,显示一个方框。
最新文章
- Android课程---序列化与反序列化(转)
- Linux Vim编辑器使用简单讲解
- CLR via C#(12)-委托Delegate
- Java Hour 56 Spring 和 Hibernate 的集成
- RabbitMQ学习总结 第四篇:发布/订阅 Publish/Subscribe
- discuzx完全自定义设计模板门户首页,栏目,专题模板方法
- selenium+testNG+Ant
- [转] JavaScript 和事件
- 修改 tomcat 内存
- jQuery动态操作表单
- 《安卓网络编程》之第八篇 安卓与服务器之间通讯JSON
- js把星期由数字转换为星期名称
- java 源码编译
- ionic基于GPS定位并通过百度地图获取定位详细信息
- 深入解析ConcurrentHashMap类
- OSS RFC
- 知识点---animate()动画滞后执行的解决方案
- sublime test3 乱码问题的解决
- 函数调用堆栈及活动记录 堆栈溢出 stack overflow
- 乐固加固后windows下实现给apk签名
热门文章
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分
- #iOS问题记录# UITextview富文本链接,禁止长按事件
- HDU 4643 GSM (2013多校5 1001题 计算几何)
- ajax跨域的解决方案
- 爬虫之多线程 多进程 自定义异步IO框架
- 流畅的python第十一章接口学习记录
- python利用opencv去除水印方法
- Spring框架学习(1)Spring简介
- git中报unable to auto-detect email address
- PHP上传文件类 代码练习