Storm
Storm

Reputation: 2022

Silverlight Runtime Binding to Custom Dependency Object

I've searched everywhere so if I'm making a double posting please forgive me.

I need a binding for the ActualHeight Property of a Border Control. I've used this post to get started: Binding to ActualWidth does not work

which in turn took me to this blog: http://darutk-oboegaki.blogspot.com/2011/07/binding-actualheight-and-actualwidth.html

where I've implemented a Class for the SizeChange and all looks good in the land of code.

My control is being created at Runtime, so I create the control, and add a Property for "IsEnabled" as per the XAML Based code

border.SetValue( SizeChange.IsEnabledProperty, true );

I've checked it by debugging the code and it's updating the SizeChange.ActualHeight correctly. Next I'm trying to bind another runtime created control to this one:

Binding weekHeight = new Binding( "SizeChange.ActualHeight" );
weekHeight.Mode = BindingMode.OneWay;
weekHeight.Source = border;
border2.SetBinding( Border.HeightProperty, weekHeight );

Now the XAML Based solution calls for a Binding of "local_ui:SizeChange.ActualHeight" but that causes a runtime error if I try that, and instead I've used "SizeChange.ActualHeight" which passes but it's not updating the size of my control.

I've tried to keep things straight forward so please forgive me if I missed something.

Many thanks!

Upvotes: 1

Views: 116

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

So the question is (in essence) how to bind a custom attached property from code-behind. The trick is to set the binding path using PropertyPath, instead of a string. Construct it using the actual dependency property (SizeChange.ActualHeightProperty), like this:

binding.Path = new PropertyPath(SizeChange.ActualHeightProperty);

So the binding constructor should look like this:

Binding weekHeight = new Binding 
{
    Path = new PropertyPath(SizeChange.ActualHeightProperty),
    Mode = BindingMode.OneWay,
    Source = border
};
border2.SetBinding( Border.HeightProperty, weekHeight );

Upvotes: 2

Related Questions