0

I am trying to bing a grid size (i.e. Width and Height) to one of its child (i.e. a Viewbox). I tried this :

Binding bind = new Binding(); bind.Source = this._vb; this._container.SetBinding(Grid.DataContextProperty, bind); 

But it did not work as expected so I tried this

Binding bindWidth = new Binding(); bindWidth.Source = this._vb.Width; this._container.SetBinding(Grid.WidthProperty, bindWidth); Binding bindHeight = new Binding(); bindHeight.Source = this._vb.Height; this._container.SetBinding(Grid.HeightProperty, bindHeight); 

I wanted to zoom the Viewbox to have its child zoomed as well and update the Grid parent of my Viewbox. I also tried to do the opposite way (zoom the grid binded to my viewbox but it did not work).

Does someone know why it happens ?

4
  • set Binding Mode bindWidth.Mode=BindingMode.TwoWay Commented Jun 13, 2014 at 13:25
  • It tells me I need Path or XPath to use a bidirectional Binding Commented Jun 13, 2014 at 13:37
  • Ah ok I tried with with the first solution I implemented. I thought it would be the same >< Commented Jun 13, 2014 at 13:41
  • It didnt change anything... I will try to find why. Thanks for your fast answers ! Commented Jun 13, 2014 at 13:45

1 Answer 1

1

Width an Height properties to guide the calculation of Width and Height. Try binding to ActualWidth/ActualHeight instead.

var binding = new Binding("ActualHeight"); binding.Source = this._vb; this._container.SetBinding("Height", binding); 

This will update the Height property on the _container to match the ActualHeight of the _vb object.

Sign up to request clarification or add additional context in comments.

1 Comment

It works great. I just needed to specify a DependencyProperty into the SetBinding function. The "Height" didn't work and I just replaced it by Grid.HeightProperty. Just for information. Anyway thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.