Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is possible through Binding like so:</p> <pre><code> &lt;TextBox Width="50" Height="20" VerticalAlignment="Top" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt; &lt;Grid Width="100" Height="100" Background="Gray" x:Name="Grid"&gt; &lt;Line Grid.Row="1" X2="100" Y2="100" Stroke="Red"&gt; &lt;Line.RenderTransform&gt; &lt;RotateTransform CenterX="50" CenterY="50" Angle="{Binding Path=Text, Mode=OneWay, Converter={StaticResource ResourceKey=SToD}}"/&gt; &lt;/Line.RenderTransform&gt; &lt;/Line&gt; &lt;/Grid&gt; </code></pre> <p>Your converter simply has to convert your string to a double <code>return System.Convert(value.ToString());</code>.</p> <p>A cool feature I discovered while testing this, is that a validation rule is automatically added which returns an ValidationError if you dont type a double value into your TextBox.</p> <p>To make this work you have to the the MainWindows DataContext to itself with <code>this.DataContext = this;</code>.</p> <p>EDIT: I wrote a little Helper class, which does the Animation for you:</p> <pre><code>public class DoubleAnimationManager { private static Dictionary&lt;DependencyObject, AnimationHelper&gt; _helperDic = new Dictionary&lt;DependencyObject, AnimationHelper&gt;(); public static PropertyPath GetProperty(DependencyObject obj) { return (PropertyPath)obj.GetValue(PropertyProperty); } public static void SetProperty(DependencyObject obj, PropertyPath value) { obj.SetValue(PropertyProperty, value); } public static readonly DependencyProperty PropertyProperty = DependencyProperty.RegisterAttached( "Property", typeof(PropertyPath), typeof(DoubleAnimationManager), new PropertyMetadata( (o, e) =&gt; { if (!_helperDic.ContainsKey(o)) _helperDic[o] = new AnimationHelper(o); _helperDic[o].Path = (PropertyPath)e.NewValue; })); public static int GetDelayTimeInMS(DependencyObject obj) { return (int)obj.GetValue(DelayTimeInMSProperty); } public static void SetDelayTimeInMS(DependencyObject obj, int value) { obj.SetValue(DelayTimeInMSProperty, value); } public static readonly DependencyProperty DelayTimeInMSProperty = DependencyProperty.RegisterAttached( "DelayTimeInMS", typeof(int), typeof(DoubleAnimationManager), new PropertyMetadata(0, (o, e) =&gt; { if (!_helperDic.ContainsKey(o)) _helperDic[o] = new AnimationHelper(o); _helperDic[o].DelayInMS = (int)e.NewValue; })); public static double GetValue(DependencyObject obj) { return (double)obj.GetValue(ValueProperty); } public static void SetValue(DependencyObject obj, double value) { obj.SetValue(ValueProperty, value); } public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( "Value", typeof(double), typeof(DoubleAnimationManager), new PropertyMetadata( (o, e) =&gt; { if (!_helperDic.ContainsKey(o)) _helperDic[o] = new AnimationHelper(o); _helperDic[o].ToValue = (double)e.NewValue; if (!_helperDic[o].Started) { _helperDic[o].FromValue = (double)e.OldValue; _helperDic[o].Start(); } })); } public class AnimationHelper { private bool _remMode; private DependencyObject _target; private BackgroundWorker _bw = new BackgroundWorker(); public double FromValue { get; set; } private double _toValue; public double ToValue { get { return _toValue; } set { if (Started) { _remMode = true; } _toValue = value; } } public bool Started { get; private set; } private int _delayInMS; public int DelayInMS { get { return _delayInMS; } set { _delayInMS = value; } } public PropertyPath Path { get; set; } public AnimationHelper(DependencyObject target) { _target = target; _bw.DoWork += delegate { System.Threading.Thread.Sleep(DelayInMS); }; _bw.RunWorkerCompleted += delegate { StartAnimation(); }; } private Storyboard GetStoryboard() { Storyboard sb = new Storyboard(); DoubleAnimation da = new DoubleAnimation(ToValue, new TimeSpan(0, 0, 3)); da.From = FromValue; Storyboard.SetTarget(da, _target); Storyboard.SetTargetProperty(da, Path); sb.Children.Add(da); sb.Completed += delegate { if (_remMode) { StartAnimation(); _remMode = false; } else { Started = false; } }; return sb; } private void StartAnimation() { GetStoryboard().Begin(); FromValue = ToValue; } public void Start() { Started = true; _bw.RunWorkerAsync(); } } </code></pre> <p>How to use it:</p> <pre><code> &lt;TextBox Width="50" Height="20" VerticalAlignment="Top" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt; &lt;Grid Width="100" Height="100" Background="Gray" x:Name="Grid"&gt; &lt;Line Grid.Row="1" X2="100" Y2="100" Stroke="Red" local:DoubleAnimationManager.Property="(UIElement.RenderTransform).(RotateTransform.Angle)" local:DoubleAnimationManager.Value="{Binding Path=Text, Mode=OneWay, Converter={StaticResource ResourceKey=SToD}}"&gt; &lt;Line.RenderTransform&gt; &lt;RotateTransform CenterX="50" CenterY="50"/&gt; &lt;/Line.RenderTransform&gt; &lt;/Line&gt; &lt;/Grid&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload