39

I have two borders with content, The second border width changes depending on the content, I'm trying to bind the first border to the second border width but it's not working and I'm not sure what i'm missing. Can someone give me some direction please? Below is a example of what I'm currently trying.

<Border x:Name="border1" Width="{Binding Path=Width, ElementName=border2}">
    ... 
</Border>

<Border x:Name="border2">
    ...
</Border>
Terco
  • 920
  • 3
  • 19
  • 34

2 Answers2

53

Change the path to ActualWidth.

<Border x:Name="border1" Width="{Binding Path=ActualWidth, ElementName=border2}">
</Border>
<Border x:Name="border2">
</Border>
Toni
  • 1,555
  • 4
  • 15
  • 23
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks. I looked up ActualWidth and found this link. http://stackoverflow.com/q/607827/684042 it makes sense to me now. thanks. – Terco May 03 '11 at 17:18
4

change path to ViewportWidth too !

<Grid x:Name="mainGrid">
//and some elements exist there
  <Grid> 
    <Border x:Name="border_btn" Width="{Binding ElementName=root ,Path=ViewportWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>  
  </Grid>
</Grid>

use ViewportWidth instead of ActualWidth because in this case ActualWidth is always shown 0! for test this problem you can test this with this line in your xaml.

  <TextBlock Text="{Binding  ElementName=mainGrid, Path=ActualWidth ,Mode=TwoWay}" Foreground="Red"/>