接收诸如.net DataBinding之类的属性更改事件 [英] Receiving Property Change Event like .net DataBinding

查看:55
本文介绍了接收诸如.net DataBinding之类的属性更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人
如您所知,使用ListControl之类的控件非常容易.带有自行开发的类的ComboBox.
我们只需要向ComboBox提供不支持IList的数据源,并设置一些ComboBox的属性即可:

Dear all
As you know it is very easy to use a ListControl like e.g. ComboBox with self developed classes.
We only have to supply a datasource which does support IList to the ComboBox and set some ComboBox’s properties:

comboBoxGloss.DataSource    = anyting which supports IList;
comboBoxGloss.DisplayMember = "DisplayPropertyName";
comboBoxGloss.ValueMember   = "ValuePropertyName";


最后但并非最不重要的一点是,为了控制所选项目,我们可以简单地添加一个DataBinding,该绑定将ComboBox的"SelectedValue"绑定到我们自己类的合适属性上.


Last but not least to control the selected item we can simply add a DataBinding which binds ComboBox’s "SelectedValue" top a suitable property of our own class.

comboBoxGloss.DataBindings.Add("SelectedValue", myClass, "MyValue");

class MyClass
{
  public int MyValue { get; set; }
}



=>因此,.NET可以连接到myclass.MyValue 的"set"方法中,而这种方式无需支持或关注任何事情..

我的问题:
我可以像.NET的DataBinding一样钩住自己的属性更改事件接收器"吗?而且这没有在相应属性的设置"方法中实现任何内容吗?另外,我正在寻找完全独立于UI的解决方案.

对不起,我的英语,请先谢谢.
关于



=> So .NET hooks into the "set" method of myclass.MyValue and this in a way in which I do not have to support or pay attention to anything.

My question:
Can I hook in the same way like .NET’s DataBinding and implement my own "property change event sink"? And this without implementing anything in the "set" method of the respective property? Additionally I''m looking for a solution which is completely UI independed.

Sorry for my english, thanks in advance.
Regards

推荐答案

不幸的是,数据绑定机制已深深地钩在控件中,因此您必须自己实现一些东西.幸运的是,在简单的情况下(绑定到一个简单的属性,并且源支持将INotifyPropertyChanged作为通知机制),这很容易做到:

Unfortunately the data binding mechanism is hooked quite deeply into controls, so you will have to implement something yourself. Fortunately, this is quite easy to do in the simple case (where you are binding to a simple property and the source supports INotifyPropertyChanged as the notification mechanism):

public class DataBinding : IDisposable {
 public INotifyPropertyChanged Source { get; private set; }
 public string SourceProperty { get; private set; }
 public object Target { get; private set; }
 public string TargetProperty { get; private set; }
 
 private PropertyInfo sourcePropertyInfo, targetPropertyInfo;

 public DataBinding(INotifyPropertyChanged source, string sourceProperty, object target, string targetProperty){
  Source = source; SourceProperty = sourceProperty;
  Target = target; TargetProperty = targetProperty;

  sourcePropertyInfo = source.GetType().GetProperty(sourceProperty);
  targetPropertyInfo = target.GetType().GetProperty(targetProperty);
 }

 public void Attach(){ 
  Detach(); // to stop double attachment
  source.PropertyChanged += SourceEventHandler;
  Refresh();
 }

 public void Detach() {
  source.PropertyChanged -= SourceEventHandler;
 }

 public void Dispose(){ Detach(); }

 private void SourceEventHandler(object sender, PropertyChangedEventArgs args){
  if(args.PropertyName == SourceProperty || args.PropertyName == null)
   Refresh();
 }

 public void Refresh(){
  targetPropertyInfo.SetValue(target, sourcePropertyInfo.GetValue(source));
 }

}



然后,您可以按照类似于
的方式使用它



You can then use this in a way analogous to

var myBinding = new DataBinding(myClass, "MyValue", myComboBox, "SelectedValue");
myBinding.Attach();



...将任何东西绑定到其他东西.

您也可以将附件代码放入构造函数中,但这对我来说似乎是不自然的.

(免责声明:这只是在邮政信箱中输入的,因此可能会出现一些小错误.)



... to bind anything to anything else.

You could also put the attachment code in the constructor, but that seems unnatural to me.

(Disclaimer: this is just typed into the posting box, so there might be minor mistakes.)


现在我找到了一种方法,但是我不确定这是否是一种干净的解决方案(样式,性能等).当然,我会将下面的示例封装在一个类中……稍后……也许.

欢迎发表评论/警告.
问候

几个小时后:
它有副作用,分别仅在某些罕见/不可接受的情况下才起作用:(

仅当"MyValue"属性绑定到ListControl时,它才有效.
仅当ListControl确实更改了"MyValue"时,它才会触发change事件.
如果将以编程方式更改"MyValue"(myClass.MyValue = 2),则不会起作用.


Now I found a way, but I''m not sure whether it is a clean solution (style, performance, ...). Of course I will encapsulate the example below in a class...later... perhaps.

Comments/warnings are welcome.
Regards

Some hours later:
It has side effects, respectively it does only work under some rare/unacceptable circumstances :(

It works only, if the "MyValue" property is bound to a ListControl.
It does only fire the change event if the ListControl does change "MyValue"
It does _not_ work if "MyValue" will be changed programmatically (myClass.MyValue= 2)


// Nothing  special to implement, simply a class with a common auto property

class MyClass
{
  // For this property I like to install a "change listener"
  public int MyValue { get; set; }
}





// Register an "change listener" to a property

class MyApplicationClass
{
  MyClass myClass= new MyClass();

  public MyApplicationClass()
  {
    // Add Listener to a specific object's property. In this case it is myClass.MyValue
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myClass);
    System.ComponentModel.PropertyDescriptor property = properties.Find("MyValue",
                                                                        false);
    property.AddValueChanged(myClass, new EventHandler(PropValueChanged)); 
  }

  private void PropValueChanged(object sender, EventArgs e)
  {
    // Here I will be informed about changes of MyClass.MyValue
  }
}


comboBoxGloss.DataSource =支持IList的任何内容;
comboBoxGloss.DisplayMember ="DisplayPropertyName";
comboBoxGloss.ValueMember ="ValuePropertyName";
comboBoxGloss.DataBind();
comboBoxGloss.DataSource = anyting which supports IList;
comboBoxGloss.DisplayMember = "DisplayPropertyName";
comboBoxGloss.ValueMember = "ValuePropertyName";
comboBoxGloss.DataBind();


这篇关于接收诸如.net DataBinding之类的属性更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆