@@ -114,6 +114,36 @@ public static void SetOffset(DependencyObject obj, string value)
114
114
obj . SetValue ( OffsetProperty , value ) ;
115
115
}
116
116
117
+ /// <summary>
118
+ /// Gets the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property of a <see cref="UIElement"/> in a <see cref="string"/> form.
119
+ /// </summary>
120
+ /// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
121
+ /// <returns>A <see cref="Vector3"/> <see cref="string"/> representation of the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property.</returns>
122
+ public static string GetTranslation ( DependencyObject obj )
123
+ {
124
+ if ( ! DesignTimeHelpers . IsRunningInLegacyDesignerMode && obj is UIElement element )
125
+ {
126
+ return GetTranslationForElement ( element ) ;
127
+ }
128
+
129
+ return ( string ) obj . GetValue ( TranslationProperty ) ;
130
+ }
131
+
132
+ /// <summary>
133
+ /// Sets the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property of a <see cref="UIElement"/> in a <see cref="string"/> form.
134
+ /// </summary>
135
+ /// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
136
+ /// <param name="value">The <see cref="string"/> representation of the <see cref="Vector3"/> to be set.</param>
137
+ public static void SetTranslation ( DependencyObject obj , string value )
138
+ {
139
+ if ( ! DesignTimeHelpers . IsRunningInLegacyDesignerMode && obj is UIElement element )
140
+ {
141
+ SetTranslationForElement ( value , element ) ;
142
+ }
143
+
144
+ obj . SetValue ( TranslationProperty , value ) ;
145
+ }
146
+
117
147
/// <summary>
118
148
/// Gets the <see cref="Visual.Opacity"/> of a UIElement
119
149
/// </summary>
@@ -334,6 +364,12 @@ public static void SetNormalizedCenterPoint(DependencyObject obj, string value)
334
364
public static readonly DependencyProperty OffsetProperty =
335
365
DependencyProperty . RegisterAttached ( "Offset" , typeof ( string ) , typeof ( VisualExtensions ) , new PropertyMetadata ( null , OnOffsetChanged ) ) ;
336
366
367
+ /// <summary>
368
+ /// Identifies the Translation attached property.
369
+ /// </summary>
370
+ public static readonly DependencyProperty TranslationProperty =
371
+ DependencyProperty . RegisterAttached ( "Translation" , typeof ( string ) , typeof ( VisualExtensions ) , new PropertyMetadata ( null , OnTranslationChanged ) ) ;
372
+
337
373
/// <summary>
338
374
/// Identifies the Opacity attached property.
339
375
/// </summary>
@@ -400,6 +436,14 @@ private static void OnOffsetChanged(DependencyObject d, DependencyPropertyChange
400
436
}
401
437
}
402
438
439
+ private static void OnTranslationChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
440
+ {
441
+ if ( e . NewValue is string str )
442
+ {
443
+ SetTranslation ( d , str ) ;
444
+ }
445
+ }
446
+
403
447
private static void OnOpacityChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
404
448
{
405
449
if ( e . NewValue is double dbl )
@@ -503,6 +547,23 @@ private static void SetOffsetForElement(string value, UIElement element)
503
547
visual . Offset = value . ToVector3 ( ) ;
504
548
}
505
549
550
+ private static string GetTranslationForElement ( UIElement element )
551
+ {
552
+ return GetVisual ( element ) . TransformMatrix . Translation . ToString ( ) ;
553
+ }
554
+
555
+ private static void SetTranslationForElement ( string value , UIElement element )
556
+ {
557
+ ElementCompositionPreview . SetIsTranslationEnabled ( element , true ) ;
558
+
559
+ Visual visual = GetVisual ( element ) ;
560
+ Matrix4x4 transform = visual . TransformMatrix ;
561
+
562
+ transform . Translation = value . ToVector3 ( ) ;
563
+
564
+ visual . TransformMatrix = transform ;
565
+ }
566
+
506
567
private static double GetOpacityForElement ( UIElement element )
507
568
{
508
569
var visual = GetVisual ( element ) ;
0 commit comments