Silverlight:以编程方式绑定控件属性 [英] Silverlight: Programmatically binding control properties

查看:25
本文介绍了Silverlight:以编程方式绑定控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大图:我有一个自定义子控件,它根据我设置的属性生成各种文本框、日期选择器、组合等.该控件嵌入在我的 SL 应用程序中的不同位置.

The big picture: I have a custom child control that generates various textboxes, datepickers, combo etc based on properties that I set. This control is embedded in various places within my SL application.

我通常使用 MVVM 模式,并且我想将这些动态控件的值绑定回我的母版页面视图模型.

I generally use the MVVM pattern, and I want to bind the values of these dynamic controls back into my master page view model.

我总是知道表单上会有 8 个控件,因此我可以为控件绑定到适当的依赖属性.然后,引用此控件的控件可以使用与已输入数据的绑定,同时保持 MVVM 模式.

I always know that there will be 8 controls on the form so I can have dependency properties in place for the controls to bind to. Then the controls that reference this control, can use binding with the data that has been entered whilst maintaining an MVVM pattern.

问题:如何以编程方式将动态控件的值绑定到依赖项属性?

The question: How can I bind the values of dynamic controls to dependency properties programmatically?

谢谢,标记

推荐答案

假设您已经动态创建了一个简单的 TextBox,并且您想在 Text 属性上添加一个绑定:-

Lets assume you have created a simple TextBox dynamically and you want to add a binding on the Text property:-

 Binding binding = new Binding("SomeProperty");
 binding.Mode = BindingMode.TwoWay;

 txtBox.SetBinding(TextBox.TextProperty, binding);

其中 txtBox 是您要观察/改变的动态创建的 TextBox.

Where txtBox is the dynamically created TextBox you want to observe/mutate.

这篇关于Silverlight:以编程方式绑定控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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