在.NET 2.0中,让你的组件也可以绑定

MS的绑定应该说是相当方便的,他可以说是最佳的MVC模式实践,关于绑定的使用和好处我就不重复说了。
但在开发中,我们发现.NET 1.1的绑定还是有一些不足的,例如,我设计了一个Command对象,我有一个Menu对象,我希望Menu的Enabled绑定到Command的Enabled属性,很可惜,我的Menu不是Control,所以没有DataBinding属性,我需要扩展让他有DataBinding属性。
在.NET 2.0中包含了IBindableComponent接口,实现此接口,就可以不是Control的东西也实现绑定,下面是其中的代码:
ContractedBlock.gif ExpandedBlockStart.gif          IBindableComponent Members #region IBindableComponent Members
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当BindingContext属性发生改变时发生
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event EventHandler BindingContextChanged;
InBlock.gif
InBlock.gif        
private BindingContext _bindingContext;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回/设置绑定环境
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
InBlock.gif        EditorBrowsable(EditorBrowsableState.Advanced),
InBlock.gif        Browsable(
false)]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public BindingContext BindingContext dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (_bindingContext == nulldot.gif{
InBlock.gif                    _bindingContext 
= new BindingContext();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _bindingContext;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (_bindingContext != value) dot.gif{
InBlock.gif                    _bindingContext 
= value;
InBlock.gif                    OnBindingContextChanged(EventArgs.Empty);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 触发BindingContext事件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e">事件需要的参数</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        protected virtual void OnBindingContextChanged(EventArgs e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (BindingContextChanged != nulldot.gif{
InBlock.gif                BindingContextChanged(
this, e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private ControlBindingsCollection _dataBinding;
InBlock.gif        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
InBlock.gif        ParenthesizePropertyName(
true),
InBlock.gif        RefreshProperties(RefreshProperties.All)]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public ControlBindingsCollection DataBindings dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (_dataBinding == nulldot.gif{
InBlock.gif                    _dataBinding 
= new ControlBindingsCollection(this);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _dataBinding;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

实现上,应该说是相当简单的,没有什么好说的。要测试此代码,可以:
            this.commandMenuItem1.DataBindings.Add(new Binding("Enabled", this.cmdSave, "Enabled"));
            this.commandMenuItem2.DataBindings.Add(new Binding("Enabled", this.cmdSave, "Enabled"));
这里,有俩个菜单绑定在同一个Command上,当你:
this.cmdSave.Enabled = !this.cmdSave.Enabled;
时,你会发现,俩个菜单自动变了。 14.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值