add render transfrom in code behind

essamce 621 Reputation points
2020-10-08T19:17:57.543+00:00

hi
here is an attached property i've created to add TranslateTransform to any control:

 public static string GetSetRanderTransformTranslateProperty(DependencyObject obj)
          => (string)obj.GetValue(SetRanderTransformTranslateProperty);
        public static void SetSetRanderTransformTranslateProperty(DependencyObject obj, string value)
            => obj.SetValue(SetRanderTransformTranslateProperty, value);
        // Using a DependencyProperty as the backing store for SetRanderTransformRotateProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SetRanderTransformTranslateProperty =
            DependencyProperty.RegisterAttached("SetRanderTrasformTranslateProperty", typeof(string), typeof(GeneralBehaviors)
                , new PropertyMetadata("0,0", OnRanderTransformTranslatePropertyChanged));

        private static void OnRanderTransformTranslatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as FrameworkElement;

            if (element == null /*|| !(e.NewValue is string)*/) return;

            element.Loaded += (s, ea) =>
            {
                var value = ((string)e.NewValue).Trim().Split(new char[] { ',', ' ' });

                if (value.Length != 2) return;
                if (double.TryParse(value[0], out double dx) &&
                    double.TryParse(value[1], out double dy))
                {
                    element.RenderTransformOrigin = new Point(0.5, 0.5);
                    TranslateTransform translate = new TranslateTransform(dx, dy);
                    element.RenderTransform = translate;
                }
                else return;
            };


        }

the problem is when i'm trying to apply ScaleTransform attached property (created the same way) to the same control it cancels the previous translateTransform.
the problem is i don't know if control.RenderTransform property is a TransfromGroup or just Transform and the check if (control.RenderTransform is TransformGroup) and if (control.RenderTransform is Transform) aren't help because RenderTransform can refer to both.
how do we solve this problem.
any help will be appreciated, thanks in advance.

i'm using VS2019 , C#, wpf .net core3.1 application.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,694 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-10-09T06:03:45.43+00:00

    From Dependency Property Setting Precedence List, your SetRanderTransformTranslateProperty is higher than control's RenderTransform, to prevent ScaleTransform from being overwritten by TranslateTransform, you can set it in under LayoutTransform. Take CheckBox using your SetRanderTransformTranslateProperty as an example:

     <CheckBox  Width="130" Height="30" Content="Click Yellow" Background="Yellow" local:GeneralBehaviors.SetRanderTransformTranslateProperty="60,60"></CheckBox>  
      
             
            <CheckBox Width="130" Height="30" Content="Click green" Background="Green" local:GeneralBehaviors.SetRanderTransformTranslateProperty="60,60">  
                <CheckBox.LayoutTransform>  
                    <ScaleTransform ScaleX="5"  ScaleY="5"></ScaleTransform>  
                </CheckBox.LayoutTransform>  
            </CheckBox>  
    

    and the result picture is:
    31029-capture.png
    If I misunderstand your question,please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments