开发者

Programatic data binding

I am trying to get my head around the power and complexities of data binding. I'm working on a reasonably complex app that requires a view model to aggregate all of the disparate data, and then bind that model to the user controls.

My view model is structured to create numerous classes to manage the individual data elements, and I want to bind these classes together programatically, i.e outside the UI.

I need to bind data eleme开发者_开发百科nts of two seemingly unrelated classes together. I can then bring the aggregate class data to the UI, using xaml.

In other words, classes "A and "B" each have a string declared as "Name". I want to achieve two way binding of these so that when either "Name" is changed, the binding takes care of the data update.

This task seems easy enough, but I have not been able to find any examples.


When it comes to databinding this MSDN page is highly relevant.

You should note that bindings require the target property to be a DependecyProperty and that bindings are conceptually one-sided, they simply can go in both directions while only being set on one end.

You normally do not want DependencyProperties is your model because it requires it to inherit from DependencyObject. It might be better to achieve this relationship by referecing the same name-wrapping object in both classes.


Binding non UI view model classes will be complicated as you will have to manually hook property changed notifications and then set your aggregate property. But MultiBinding was specially designed for this scenario.

Instead of creating aggregate class property/class you should try MultiBinding, http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

Another way is to create MultiBinding easily with following LambdaBinding Extensions.

Disclaimer: I will be talking about my company's product here.

We have something called Lambda Binding which allows you to bind complex expressions involving other controls and any other data items.

http://uiatoms.neurospeech.com/2011/03/ui-atoms-1-7-5-released/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜