WPF: Binding to readonly property in code?

You cannot do it this way. You cannot set a Binding to ActualWidth as it's read-only You can only set a binding to MyWidth But for this, you need first to convert MyWidth into a DependencyProperty Then you will be able to do something like Binding be = new Binding("ActualWidth") { Source = myItemsControl }; this. SetBinding(MyViewModel.

MyWidthProperty, b) For converting into a dependency property, you'll need to replace your definition of MyWidth with the following: public static readonly DependencyProperty MyWidthProperty = DependencyProperty. Register("MyWidth", typeof(double), typeof(MyViewModel), new UIPropertyMetadata( 0.0, (d, e) => { var self = (MyViewModel)d; ViewChanged(self); })) But be careful with dependency properties; it's better to read the documentation first Edit: You would also need to define the property this way: public double MyWidth { get { return (double)this. GetValue(MyWidthProperty); } set { this.

SetValue(MyWidthProperty, value); } }.

You cannot do it this way. You cannot set a Binding to ActualWidth, as it's read-only. You can only set a binding to MyWidth.

But for this, you need first to convert MyWidth into a DependencyProperty. Then you will be able to do something like Binding be = new Binding("ActualWidth") { Source = myItemsControl }; this. SetBinding(MyViewModel.

MyWidthProperty, b); For converting into a dependency property, you'll need to replace your definition of MyWidth with the following: public static readonly DependencyProperty MyWidthProperty = DependencyProperty. Register("MyWidth", typeof(double), typeof(MyViewModel), new UIPropertyMetadata( 0.0, (d, e) => { var self = (MyViewModel)d; ViewChanged(self); })); But be careful with dependency properties; it's better to read the documentation first. Edit: You would also need to define the property this way: public double MyWidth { get { return (double)this.

GetValue(MyWidthProperty); } set { this. SetValue(MyWidthProperty, value); } }.

Thanks for that very complete answer. Been avoiding dependency properties due to the nasty syntax whilst I made progress with the rest of WPF. First time I have been forced to use one... looks like I'd better get on top of them as I expect it will happen again.

– rjw Nov 16 '10 at 19:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions