简体   繁体   English

单向绑定有效,两向绑定无效

[英]One-Way Binding works, Two-Way Binding doesn't

In my Silverlight prjoect I am creating a simple two-way binding in code: 在我的Silverlight开发中,我正在代码中创建一个简单的双向绑定:

This is the property I want to bind to: 这是我要绑定的属性:

public class Selection : ViewModelBase {

    private string selectedModel;

    public string SelectedModel {
        get { return selectedModel; }
        set {
            selectedModel = value;
            FirePropertyChanged("SelectedModel");
        }
    }
}

My binding target is the text property of a rich text box. 我的绑定目标是富文本框的text属性。

My binding code is the following: 我的绑定代码如下:

Selection s = getSelectionObject();

Binding modelBinding = new Binding("SelectedModel");
modelBinding.Source = s.SelectedModel;
selectorContent.usc_ModelSelector.SetBinding(GSelector.TextProperty, modelBinding);

That works fine! 很好! Every time the SelectedModel property of the Selection object changes, the rich text box is getting notified and updated accordingly. 每当Selection对象的SelectedModel属性更改时,富文本框都会得到通知并相应地进行更新。 But it's only bound one-way and I need it bound two-way. 但是它仅是单向绑定的,我需要将它绑定到双向。 So i have tried to change the binding mode by altering the above code: 所以我试图通过更改上面的代码来更改绑定模式:

Binding modelBinding = new Binding("SelectedModel");
modelBinding.Path = new PropertyPath(s.SelectedModel);
modelBinding.Source = s.SelectedModel;
modelBinding.Mode = BindingMode.TwoWay;
selectorContent.usc_ModelSelector.SetBinding(GSelector.TextProperty, modelBinding);

But that doesn't work. 但这是行不通的。 It compiles but the whole binding now seems to be without effect. 它可以编译,但是现在整个绑定似乎无效。 Not only that I can't trigger a change in the ViewModel (in the Selection class in the SelectedModel property - which should be the effect when two-way binding would work correctly) when changing the text of the RichTextBox, I even can't see the one-way binding wokring any more which worked with the above code! 更改RichTextBox的文本时,不仅无法触发ViewModel的更改(在SelectedModel属性的Selection类中-当双向绑定将正常工作时应该起作用),我什至不能请参阅与上述代码配合使用的单向绑定,然后开始工作!

It seems that the line 看来线

modelBinding.Path = new PropertyPath(s.SelectedModel);

is the source of the problem because when I set s.SelectedModel as the bindings Path property then the Path property of the modelBinding object seems to have the correct value (which revealed a look inside the object during runtine while debugging - it is a string value) but I can't see any change in the rich text box. 这是问题的根源,因为当我将s.SelectedModel设置为bindings的Path属性时,modelBinding对象的Path属性似乎具有正确的值(调试时在runtine期间显示了对象内部的外观-它是字符串值),但在富文本框中看不到任何更改。

Restoring the code to the one-way binding version immediately makes the rich text box work again as it should. 立即将代码还原为单向绑定版本,可以使富文本框再次正常工作。

Can anyone help ? 有人可以帮忙吗? I already spent several hours in this and i really have to catch a customer's deadline .. So any help would be highly appreciated :) Many thanks in advance ... 我已经花了几个小时了,我真的必须赶上客户的截止日期..因此,任何帮助将不胜感激:)预先感谢...

This: 这个:

new Binding("SelectedModel");

Creates a new binding with the path "SelectedModel" . 用路径"SelectedModel"创建一个新的绑定。 Then you go on to overwrite the correct path with this line: 然后,您继续使用此行覆盖正确的路径:

modelBinding.Path = new PropertyPath(s.SelectedModel);

Which is not going to work; 这是行不通的; The constructor reference states: 构造函数参考指出:

A property path that either describes a path to a common language runtime (CLR) property, or a single dependency property. 属性路径,它描述了公共语言运行时(CLR)属性的路径,或者是单个依赖项属性。

So either use a string or a reference to a DP. 因此,请使用字符串或对DP的引用。

Anyway, i do not think that you need that line at all because you already set the path correctly in the binding constructor. 无论如何,我认为您根本不需要该行,因为您已经在绑定构造函数中正确设置了路径。

Edit: Your source is wrong! 编辑:您的来源有误!

The source should be the object containing the property, here that is s . 源应该是包含属性的对象,这里是s

Binding modelBinding = new Binding("SelectedModel");
modelBinding.Source = s;
modelBinding.Mode = BindingMode.TwoWay; // This might be optional depending on the default mode of the target property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM