博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Wpf 自定义控件(1)
阅读量:6900 次
发布时间:2019-06-27

本文共 1827 字,大约阅读时间需要 6 分钟。

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
>
 
这样就完成自定义控件的第一步了即,显示一个方框。
 
 
 
 

转载于:https://www.cnblogs.com/cody1988/p/3771783.html

你可能感兴趣的文章
【LeetCode】107 - Binary Tree Level Order Traversal II
查看>>
大叔也说Xamarin~Android篇~原生登陆与WebView的网站如何共享Session
查看>>
开发人员应该对IIS理论层的知识了解的多一些~第三讲 网页从IIS端到客户端浏览器经历的阶段...
查看>>
将不确定变为确定~老赵写的CodeTimer是代码性能测试的利器
查看>>
Android自定义文本的进度条
查看>>
How to call JavaScript Function in objective C
查看>>
Javascript刷新页面的几种方法
查看>>
JS中apply和call的联系和区别
查看>>
十分钟让你的javascript登峰造极
查看>>
dotnet run 段错误
查看>>
怎样在RedHat Linux上使用oracle-validated包
查看>>
模拟web高并发
查看>>
解决数据重复导致查询出错问题
查看>>
springmvc(4)注解简单了解
查看>>
HTMLCSS学习笔记(四)----浮动原理及清浮动
查看>>
探究如何求两数的最大公约数(两种方法)
查看>>
Better Django models
查看>>
第 8 章 容器网络 - 054 - 准备 macvlan 环境
查看>>
第 10 章 容器监控 - 081 - Weave Scope 多主机监控
查看>>
FLASK爬坑笔记
查看>>