.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
Why is that every time i assing RenderTransform from the .cs file the XAML binding stop working?
Why is that every time i assing RenderTransform from the .cs file the XAML binding stop working?
- 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
- 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.- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 2:24 AM
- 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.
- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 2:24 AM
- 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.
- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 8:08 PM
All Replies
- 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.- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 2:24 AM
- 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.
- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 2:24 AM
- 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. - 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.
- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 06, 2009 3:43 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 8:08 PM
- Hi,
yes, that's right...


