Expression > Forums Home > Related Technologies Forums > Windows Presentation Foundation (WPF) > Why is that every time i assing RenderTransform from the .cs file the XAML binding stop working?
Ask a questionAsk a question
 

AnswerWhy is that every time i assing RenderTransform from the .cs file the XAML binding stop working?

  • Sunday, November 01, 2009 1:24 PMEitanGa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    As you can see i have rectangle that is binding to a slider (X axis);
    When i press the button, I assign to the rectangle transformation new TranslateTransform.
    From that point, the binding between the slider and the rectangle stop working.
    Why is that ? and how can i renew the binding ?

    <Window x:Class="TransformationEx.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TransformationEx" Height="300" Width="500"
        >
    	<Grid>
    		<StackPanel>
    			<Slider x:Name="TheSlider" Value="0" Maximum="200" Minimum="-200"/>
    			<Button Content="Press" Width="80" Click="OnClick"/>
    		</StackPanel>
    		<Rectangle x:Name="MyRect" Fill="LightBlue" Width="50" Height="50">
    			<Rectangle.RenderTransform>
    				<TransformGroup>
    					<TranslateTransform X="{Binding ElementName=TheSlider, Path=Value}"/>
    				</TransformGroup>
    			</Rectangle.RenderTransform>
    		</Rectangle>
    	</Grid>
    </Window>
    
    

    And the OnClick method:
    		public void OnClick(object sender, RoutedEventArgs e)
    		{
    			TranslateTransform translateTransform = new TranslateTransform(80, 80);
    			TransformGroup transformGroup = new TransformGroup();
    			transformGroup.Children.Add(translateTransform);
    
    			MyRect.RenderTransform = transformGroup;
    		}
    


Answers

  • Sunday, November 01, 2009 7:26 PMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    the binding stops working because it is defined on one Transform object, and in your click event handler you replace it with another Transform object, which has no Binding defined. If you want to restore the Binding, save the value of MyRect.RenderTransform before you overwrite it, and restore it later when you want the binding to work again. Or even just add it to the TransformGroup, then the element will first be translated by 80, 80, and then it can be moved around this new position through the Slider.
  • Wednesday, November 04, 2009 2:24 AMBrendan Clark - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is a new feature in .NET 4.0 that will allow you to more elegantly work-around this scenario.  A new method was added to DependencyObject called SetCurrentValue which allows you to set a value for a DependencyProperty without removing its binding - this value will persist until the binding propagates a new value, or another value is explicitly set.
  • Wednesday, November 04, 2009 8:08 PMBrendan Clark - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You're right, this wouldn't work if you re-created the transform.  However, I'm not sure I understand why that's necessary since all the code is doing is replacing one TranslateTransform with another.  If instead OnClick used SetCurrentValue on the existing transform, you'd get the behavior you want - when you click the button the values would be set, but when you move the slider the binding would still be in place and would be respected as well.

All Replies

  • Sunday, November 01, 2009 7:26 PMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    the binding stops working because it is defined on one Transform object, and in your click event handler you replace it with another Transform object, which has no Binding defined. If you want to restore the Binding, save the value of MyRect.RenderTransform before you overwrite it, and restore it later when you want the binding to work again. Or even just add it to the TransformGroup, then the element will first be translated by 80, 80, and then it can be moved around this new position through the Slider.
  • Wednesday, November 04, 2009 2:24 AMBrendan Clark - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is a new feature in .NET 4.0 that will allow you to more elegantly work-around this scenario.  A new method was added to DependencyObject called SetCurrentValue which allows you to set a value for a DependencyProperty without removing its binding - this value will persist until the binding propagates a new value, or another value is explicitly set.
  • Wednesday, November 04, 2009 5:13 AMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    while this will be a very useful feature, it is not related to this particular problem, since you may notice that the binding is defined on the TranslateTransform object's X property, not on the Rectangle's RenderTransform property, so that there is actually no binding on this property that could restore its value, and there is no way to restore the binding other than restoring the reference to the original Transform, or creating a new one with the same binding.
  • Wednesday, November 04, 2009 8:08 PMBrendan Clark - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You're right, this wouldn't work if you re-created the transform.  However, I'm not sure I understand why that's necessary since all the code is doing is replacing one TranslateTransform with another.  If instead OnClick used SetCurrentValue on the existing transform, you'd get the behavior you want - when you click the button the values would be set, but when you move the slider the binding would still be in place and would be respected as well.
  • Thursday, November 05, 2009 5:26 AMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    yes, that's right...