开发者

Binding a (complex) class property to CF.NET in C#

开发者 https://www.devze.com 2023-01-12 19:49 出处:网络
I am trying to make a 2-way binding of a class property. public class MyClass{ public MyField AField1{get;set;};

I am trying to make a 2-way binding of a class property.

public class MyClass{
public MyField AField1{get;set;};
public MyField AField2{get;set;};
}

public class MyField{
public string Value {get; set}
}

MyClass _class = MyClass();
_dv.DataSource = _class;

Databinding text object开发者_运维百科 displays MyField class name instead of Value Property. I also tried to enter:

DataMember = "AField1.Value";

Is there any way to bind (2-way) AField1.Value of a class MyClass to a visual control?


It's a pain. There's no built-in way to achieve this in .NET, so I can safely say, even less in the CF.

You can get started with this article on MSDN Blogs, but it's pretty limited as you can only get one level of nested property bindings.

Personnally, I ended up writing a custom BindingSource, based on code that lies somewhere on the internets. I can't give you source code of my rewrite as it's property of my employer, but here's the link to the project that got me started.

There are a few drawbacks to the code provided : some of his namespaces are System.ComponentModel, and VS2010 didn't seem to like, so I had to rename them. And a few more issues in design time that can make it a pain to use (so you'll want to fix that too), like loosing the list of properties when you make a spelling mistake and so on...

But it's the best shot (IMHO) at creating a good BindingSource that handles nested objects, and you'll get the idea of what needs to be done to achieve your nested bindings.

The last drawback (and biggest probably, but I have no experience with CF) is that the project is written for the regular .NET Framework, so it's likely that you will have to rewrite it entirely.

Hope that helps....

Edit. Uh oh, I've been grave-digging without noticing... sorry.

PS. Another idea is to simply create one binding source for each of your nested objects, but it gets messy (IMO) if your object hierarchy is complex.

0

精彩评论

暂无评论...
验证码 换一张
取 消