Reputation: 5886
In my Silverlight prjoect I am creating a simple two-way binding in code:
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.
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. 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!
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.
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 ...
Upvotes: 0
Views: 1771
Reputation: 184376
This:
new Binding("SelectedModel");
Creates a new binding with the path "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.
So either use a string or a reference to a 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
.
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
Upvotes: 1