Mar*_*per 6 silverlight binding mvvm
大图:我有一个自定义子控件,可以根据我设置的属性生成各种文本框,日期选择器,组合等.此控件嵌入在SL应用程序中的各个位置.
我通常使用MVVM模式,我想将这些动态控件的值绑定回我的母版页视图模型.
我总是知道窗体上会有8个控件,所以我可以为控件绑定到依赖属性.然后,引用此控件的控件可以使用与已保存MVVM模式时输入的数据绑定.
问题:如何以编程方式将动态控件的值绑定到依赖项属性?
谢谢,马克
Ant*_*nes 10
让我们假设您已动态创建一个简单的TextBox,并且您想在Text属性上添加一个绑定: -
Binding binding = new Binding("SomeProperty");
binding.Mode = BindingMode.TwoWay;
txtBox.SetBinding(TextBox.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)
其中txtBox是您想要观察/变异的动态创建的TextBox.
马克,我不完全确定我已经理解了你问题中的含义,但是你考虑过Binding类吗?例如:
Customer customer = new Customer();
TextBox box = new TextBox();
Binding binding = new Binding("FullName");
binding.Source = customer;
box.SetBinding(TextBox.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)
这将TextBox控件的"Text"依赖项属性绑定到客户对象的"FullName"属性.