locked
Updating databound properties when source is changed RRS feed

  • Question

  • I have two Border elements for which I want the second one to have the same width & height as the first one, so I figured binding would be the best solution. My XAML is as follows:
    <Style x:Key="MotorSensorTestBorderStyle" TargetType="Border">
    	<Setter Property="BorderThickness" Value="4"/>
    	<Setter Property="BorderBrush" Value="White"/>
    	<Setter Property="Background" Value="Gray"/>
    	<Setter Property="CornerRadius" Value="10"/>
    	<Setter Property="Margin" Value="5"/>
    	<Setter Property="Padding" Value="5"/>
    	<Setter Property="HorizontalAlignment" Value="Center"/>
    </Style>

    <Border x:Name="bdrTesting" Grid.Row="2" Style="{StaticResource MotorSensorTestBorderStyle}" Padding="0"><Image x:Name="imgTesting" HorizontalAlignment="Center" Source="WIN_20131027_215125.JPG"/></Border>
    <Border x:Name="bdrMask" Grid.Row="2" Style="{StaticResource MotorSensorTestBorderStyle}" Background="Transparent" Height="{Binding ActualHeight,ElementName=bdrTesting}" Width="{Binding ActualWidth,ElementName=bdrTesting}"/>
    I want the second Border to have it's Width & Height updated when I set the Source property of the first Border. However, this does not seem to be happening. What am I forgetting? Do I need to do the updating manually? Any help would be appreciated. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/


    Thursday, October 31, 2013 7:10 PM

Answers

  • ActualHeight and ActualWidth are calculated properties which do not post change notifications. They are not suitable for databinding. This is called out in the remarks in their documentation:

    For purposes of ElementName binding, ActualWidth does not post updates when it changes (due to its asynchronous and run-time calculated nature). Do not attempt to use ActualWidth as a binding source for an ElementName binding. If you have a scenario that requires updates based on ActualWidth, use a SizeChanged handler.

    --Rob

    Thursday, October 31, 2013 7:39 PM
    Moderator

All replies

  • ActualHeight and ActualWidth are calculated properties which do not post change notifications. They are not suitable for databinding. This is called out in the remarks in their documentation:

    For purposes of ElementName binding, ActualWidth does not post updates when it changes (due to its asynchronous and run-time calculated nature). Do not attempt to use ActualWidth as a binding source for an ElementName binding. If you have a scenario that requires updates based on ActualWidth, use a SizeChanged handler.

    --Rob

    Thursday, October 31, 2013 7:39 PM
    Moderator
  • OK, that explains why my code wasn't working. I have my code working by manually setting the Width & Height in the SizeChanged handler of the first Border. Although that is very simple code, it would be nice to do it using databinding so that I could keep it in the XAML. Is there a way to do this using databinding, or is the SizeChanged handler my only solution? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, October 31, 2013 7:57 PM
  • You could write a behavior to listen to SizeChanged and do the update and then reference the behavior in your Xaml. It may be possible to do this with the behaviors included in Blend, but I haven't checked.

    --Rob

    • Proposed as answer by aiyaiya Monday, November 4, 2013 2:30 AM
    Thursday, October 31, 2013 8:22 PM
    Moderator