博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PropertyGrid无意的发现DisplayNameAttribute及应用
阅读量:7111 次
发布时间:2019-06-28

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

     说到这个winform属性控件,要从以前参与项目架构研发,做报表引擎开始,当时我们的目的是想做一个比较简单的报表引擎没有RDLC等报表复杂,是为了让我们的可以操作,用户可以凭借表单拖拽设置样式完成报表模板设置,与我们开发人员代码实现的数据流结合,产生基于apose.cells的excel报表。把我们多而烦的报表业务交给用户完成,用户利用报表设计完成的模板保存为xml保存至服务器,工以后使用。当时做到表单控件属性设计无疑我们采用了控件,支持很多的控件设计时交互,很强大,但是对于控件的属性汉化却存在问题,当时也没仔细查阅msdn,直接让控件属性为中文字段设计完成,对用户提示。最后项目基本完成,能够应对简单报表,稍微减少了些开发工作量吧。

     在时隔今天做基本控件的封装的时候突然看见了System.ComponentModel.DisplayNameAttribute这个标签,见名就考虑是做什么的,查询msdn才知道他就可以完成对的现实名称修改()。于是便尝试了一下。

代码:

ExpandedBlockStart.gif
View Code
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace WindowsFormsApplication1 
    
class TestControl : System.Windows.Forms.TextBox, Green.SmartUIControls.ISmartUIControl 
    { 
        [System.ComponentModel.Browsable(
true)] 
        [System.ComponentModel.DefaultValue(
null)] 
        [System.ComponentModel.Description(
"
数据绑定匹配属性
")] 
        [System.ComponentModel.Category(
"
Green.SmartUIControl
")] 
        [System.ComponentModel.DisplayName(ControlResource.Data)] 
        
public 
string Data 
        { 
get
set; } 
        
#region ISmartUIControl 成员 
        
private Green.SmartUIControls.IDataBindSetting _DataBindSetting; 
        [System.ComponentModel.Browsable(
false)] 
        
public Green.SmartUIControls.IDataBindSetting DataBindSetting 
        { 
            
get 
            { 
                
if (_DataBindSetting == 
null && !
this.DesignMode) 
                { 
                    _DataBindSetting = 
new Green.SmartUIControls.DefaultDataBindSetting(
this); 
                } 
                
return _DataBindSetting; 
            } 
            
set 
            { 
                
if (value != 
null
                { 
                    _DataBindSetting = value; 
                } 
            } 
        } 
        [System.ComponentModel.Browsable(
true)] 
        [System.ComponentModel.DefaultValue(
null)] 
        [System.ComponentModel.Description(
"
数据绑定匹配属性
")] 
        [System.ComponentModel.Category(
"
Green.SmartUIControl
")] 
        [System.ComponentModel.DisplayName(ControlResource.DataBindProperty)] 
        
public 
string DataBindProperty 
        { 
            
get
            
set
        } 
        [System.ComponentModel.Browsable(
true)] 
        [System.ComponentModel.DefaultValue(
null)] 
        [System.ComponentModel.Description(
"
Dock
")] 
        [System.ComponentModel.Category(
"
Green.SmartUIControl
")] 
        [System.ComponentModel.DisplayName(ControlResource.Dock)] 
        
public 
override System.Windows.Forms.DockStyle Dock 
        { 
            
get 
            { 
                
return 
base.Dock; 
            } 
            
set 
            { 
                
base.Dock = value; 
            } 
        } 
        
#endregion 
    } 
    
public 
class ControlResource 
    { 
#if Debug 
        
public 
const 
string Dock = 
"
Dock
"
        
public 
const 
string DataBindProperty = 
"
DataBindProperty
"
        
public 
const 
string Data = 
"
Data
"
#else 
        
public 
const 
string Dock = 
"
停靠
"
        
public 
const 
string DataBindProperty = 
"
数据绑定匹配属性
"
        
public 
const 
string Data = 
"
数据
"
#endif 
    } 
}

    最后需要特别说明的是对于Attribute我们只能传入常量。在我们的很多开发员使用控件等时候我们也许都习惯了英文对于中文不适应了,但是我们可以利用vs的条件编译绕过,编译出不同的dll包,开发版和用户使用版本。如上面的对于调试和发布版的显示设置。这个ControlResource我们可以开发一个简单的工具对其xml保存并生成我们需要的代码维护。

看图:

设置Dock=Top:

设置Dock=Bottom:

    同时我也考虑到在我们的工作流自定义表单设计和代码生成工具等中我们也可以运用,表单设计的控件字段属性设置,保存为xml或者二级制,xaml存储之类。简单说一句对于silverlight,wpf 的对于我觉得保存为xaml是最简单的,我们可以直接保存xaml,并简单转换加入父容器中。

转载地址:http://fblhl.baihongyu.com/

你可能感兴趣的文章
JSF---->其他标签
查看>>
Add Console Application Program to the MFC Program
查看>>
Oracle中可被并行化执行的SQL操作
查看>>
新的Layout布局系统
查看>>
java链表
查看>>
VC获取操作系统版本和名称
查看>>
禁止复制
查看>>
使用GLSL实现更多数量的局部光照 【转】
查看>>
rundll32命令大全
查看>>
OC 内存管理-02 autorelease 概念 以及用法
查看>>
IPC——匿名管道
查看>>
AsyncSocket长连接棒包装问题解决
查看>>
ios调用dismissViewController的一个小陷阱
查看>>
[Android Pro] static 和 Volatile 的区别
查看>>
深入理解PHP内核(八)变量及数据类型-预定义变量
查看>>
linker command failed with exit code 1 (use -v to see invocation)
查看>>
播放后台音频
查看>>
多线程 这一篇仔细看
查看>>
Nokia 920板砖自救(理论上通用,升级Win10成板砖也可以用这个恢复)
查看>>
Anroid搭建一个局域网Web服务器
查看>>