Reputation: 41
I'm trying to bind the Width property of a Canvas to the Width property of a Shape instance. So the Shape Width should be updated when the Canvas Width gets a new value
I want to do that in code, without XAML, because I create these elements on runtime.
I tried this, but it didnt work (the code is inside of my Canvas):
Binding binding = new Binding();
binding.Mode = BindingMode.OneTime;
binding.Source = this;
binding.Path = new PropertyPath("Width");
shape.SetBinding(FrameworkElement.WidthProperty, binding);
Thanks a lot for your help!
ksman
Upvotes: 0
Views: 1001
Reputation: 6103
OneTime
looks wrong. I think you want to use OneWay
or TwoWay
. Check the BindingModes
Edit
Since OneWay
and ActualWidth
didn't fix your problem, I should recommend you use a tool for debugging bindings. I use Snoop because it is free, but there are others. Debugging WPF without a tool like this can be painful.
Upvotes: 2
Reputation: 97656
You probably want to bind to ActualWidth
and not Width
.
Width
is an input into the layout process -- it's something you may (but don't have to) specify.
ActualWidth
is an output of the layout process -- it's the actual width that you see on the screen. This is partly determined by Width
, if present, but other factors go into it too (especially if you didn't explicitly set Width
in your XAML).
Upvotes: 1