我打算在我的几个类之间使用数据绑定.换句话说,我不是模型类和UI之间的绑定值,而是绑定不同类之间的变量.
我已经在几个地方读过C#中的数据绑定,但大多数都是指Windows Form和源对象之间的绑定.
我还是C#的新手.这就是我理解我应该做的事情:
首先,对于我的源对象,比如说有一个类名DataObject.源对象必须实现一个INotifyPropertyChange接口,然后在属性health设置为更改时触发事件.我对此没有任何问题.
现在,假设我有一个名为的目标对象CharacterClass.life是一个属性CharacterClass,并且也是目标属性,我希望绑定到源对象的health属性.
如何只在普通的.NET框架中将两个属性(单向和双向)绑定在一起?
关于为什么我问这个问题的一些背景信息:
万一你认为这是一个重复的问题,但事实并非如此.我搜索过SE.关于代码中数据绑定的其他问题是在WPF或XAML的上下文中,这不适合我.我还在MSDN上阅读了几篇文章,似乎我可以创建一个Binding对象,然后通过绑定源和目标BindingOperations.SetBinding().但是,Binding该类似乎是命名空间下的WPF库的一部分 System.Windows.Data.Binding.虽然我正在使用C#,但我怀疑我是否可以轻松访问WPF库,因为我主要使用C#作为Unity3D中的脚本语言.我相信我只能访问vanilla .Net框架.但是,我对此并不十分肯定,因为我还是C#的新手.
尽管对使用的UI框架紧密耦合的绑定有很多支持,但您仍然可以轻松编写自己的绑定框架.
这是一个POC,它实现了两个对象属性之间的单向绑定.
注意:这只是一种可能的方式,最好是POC(可能需要对高性能/生产场景进行微调),并使用.Net 2.0类和接口,不依赖于任何UI框架('vanilla'.net框架在你的话:)).一旦理解了这一点,您就可以轻松扩展它以支持双向绑定
class Program
{
public static void Main()
{
Source src = new Source();
Destination dst = new Destination(src);
dst.Name = "Destination";
dst.MyValue = -100;
src.Value = 50; //changes MyValue as well
src.Value = 45; //changes MyValue as well
Console.ReadLine();
}
}
//A way to provide source property to destination property
//mapping to the binding engine. Any other way can be used as well
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
internal class BindToAttribute : Attribute
{
public string PropertyName
{
get;
private set;
}
//Allows binding of different properties to different sources
public int SourceId
{
get;
private set;
}
public BindToAttribute(string propertyName, int sourceId)
{
PropertyName = propertyName;
SourceId = sourceId;
}
}
//INotifyPropertyChanged, so that binding engine knows when source gets updated
internal class Source : INotifyPropertyChanged
{
private int _value;
public int Value
{
get
{
return _value;
}
set
{
if (_value != value)
{
_value = value;
Console.WriteLine("Value is now: " + _value);
OnPropertyChanged("Value");
}
}
}
void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
internal class Destination
{
private BindingEngine<Destination> _binder;
private int _myValue;
[BindTo("Value", 1)]
public int MyValue
{
get
{
return _myValue;
}
set
{
_myValue = value;
Console.WriteLine("My Value is now: " + _myValue);
}
}
//No mapping defined for this property, hence it is not bound
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
Console.WriteLine("Name is now: " + _name);
}
}
public Destination(Source src)
{
//Binder for Source no 1
_binder = new BindingEngine<Destination>(this, src, 1);
}
}
internal class BindingEngine<T>
{
private readonly T _destination;
private readonly PropertyDescriptorCollection _sourceProperties;
private readonly Dictionary<string, PropertyDescriptor> _srcToDestMapping;
public BindingEngine(T destination, INotifyPropertyChanged source, int srcId)
{
_destination = destination;
//Get a list of destination properties
PropertyDescriptorCollection destinationProperties = TypeDescriptor.GetProperties(destination);
//Get a list of source properties
_sourceProperties = TypeDescriptor.GetProperties(source);
//This is the source property to destination property mapping
_srcToDestMapping = new Dictionary<string, PropertyDescriptor>();
//listen for INotifyPropertyChanged event on the source
source.PropertyChanged += SourcePropertyChanged;
foreach (PropertyDescriptor property in destinationProperties)
{
//Prepare the mapping.
//Add those properties for which binding has been defined
var attribute = (BindToAttribute)property.Attributes[typeof(BindToAttribute)];
if (attribute != null && attribute.SourceId == srcId)
{
_srcToDestMapping[attribute.PropertyName] = property;
}
}
}
void SourcePropertyChanged(object sender, PropertyChangedEventArgs args)
{
if (_srcToDestMapping.ContainsKey(args.PropertyName))
{
//Get the destination property from mapping and update it
_srcToDestMapping[args.PropertyName].SetValue(_destination, _sourceProperties[args.PropertyName].GetValue(sender));
}
}
}
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
5530 次 |
| 最近记录: |