c# propertyGrid属性控件跟Htmlelement元素绑定(原创精华)

加入一个propertyGrid

PropertyGrid propertyGrid1 = new PropertyGrid();
propertyGrid1.CommandsVisibleIfAvailable 
= true;
            propertyGrid1.Location 
= new Point(1020);
            propertyGrid1.Size 
= new System.Drawing.Size(400300);
            propertyGrid1.TabIndex 
= 1;
            propertyGrid1.Text 
= "Property Grid";

            
this.Controls.Add(propertyGrid1);

下面是事件触发
我们对<label>元素做例子

private void editocx_onDocumentmousedown()
        
{
            mshtml.IHTMLElement ele
=editocx.getCurElement();
            
if(ele==null)return;
            
switch(ele.tagName.ToLower())
            
{
                
case "label":
                AdapterProperty(
new LabelElement(ele));
                
break;
                    //省略其他
                
default:
                            
break;
            }

        }

AdapterProperty把对象赋值给propertyGrid的selectobject属性

private void AdapterProperty(object eleclass)
        
{
    propertyGrid1.SelectedObject
=eleclass;
        }

下面是htmlelement属性适配类,以label举例,并且对label的id和name这两个属性绑定

//Add by Alice.2005-7-8
public class PropertyAdapter
    
{
        
public PropertyAdapter()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
public static string getEleAttr(IHTMLElement e,string name)
        
{
            
if(e.getAttribute(name,0)==null)return "";
            
return e.getAttribute(name,0).ToString();
        }

        
public static void setEleAttr(IHTMLElement e,string name,string _value)
        
{
            
if(e.getAttribute(name,0)!=null){
                
if(_value.Equals("")||_value==null)
                
{
                    e.removeAttribute(name,
0);
                    
return;
                }

            e.setAttribute(name,_value,
0);
            
return;
            }

            
if(_value.Equals("")||_value==null)return;
            e.setAttribute(name,_value,
0);
        }

    }

    
public class LabelElement
    
{
        
private IHTMLElement e;
        [DescriptionAttribute(
"The rate in milliseconds that the text will repeat."),
        CategoryAttribute(
"Global Settings")]
        
public string id
        
{
            
get 
            
{
                
return e.id;
            }

            
set
            
{
                e.id
=value;
            }

        }

        [CategoryAttribute(
"Global Settings")]
        
public string name
        
{
            
get 
            
{
                
return PropertyAdapter.getEleAttr(e,"name");
            }

            
set
            
{
                PropertyAdapter.setEleAttr(e,
"name",value);
            }

        }

        
public LabelElement(IHTMLElement ele)
        
{
            e
=ele;
        }

    }


 

原文地址:https://www.cnblogs.com/alice/p/188661.html