Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing binding in VisualState Storyboard
    text
    copied!<p>I am writing a custom control for a WPF application. I want to use a color animation in a <code>Storyboard</code> in a <code>VisualState</code> definition. The <code>To</code> property of that animation should be bound to a dependency property of my control object. This does not appear to work. </p> <p>I have found a thread in a Silverlight forum describing the exact same problem, in which it is claimed that this works in SL4 RTM: <a href="http://forums.silverlight.net/forums/p/174655/423324.aspx" rel="nofollow">http://forums.silverlight.net/forums/p/174655/423324.aspx</a>. However, when I try using the code posted in my VS2010 WPF application then it does not work, meaning that the color does not change. The only binding I have been able to do within a <code>VisualState</code> <code>Storyboard</code> is to <code>StaticResource</code>.</p> <p>Any ideas?</p> <p>EDIT: </p> <p>Added code snippets:</p> <p>from Generic.xaml:</p> <pre><code>&lt;Style TargetType="{x:Type local:TestCustomControl}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:TestCustomControl}"&gt; &lt;Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Name="MyBorder"&gt; &lt;Border.Background&gt; &lt;SolidColorBrush Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ColdColor}" /&gt; &lt;/Border.Background&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="CommonStates"&gt; &lt;VisualState x:Name="Normal"/&gt; &lt;VisualState x:Name="MouseOver"&gt; &lt;Storyboard&gt; &lt;!-- This works: --&gt; &lt;!--&lt;ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="Red" Duration="0:0:0.2"/&gt;--&gt; &lt;!-- This also works: --&gt; &lt;!--&lt;ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{StaticResource HotColorRes}" Duration="0:0:0.2"/&gt;--&gt; &lt;!-- This doesn't work: --&gt; &lt;ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HotColor}" Duration="0:0:0.2"/&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;/VisualStateGroup&gt; &lt;/VisualStateManager.VisualStateGroups&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>TestCustomControl.cs:</p> <pre><code>public class TestCustomControl : Button { static TestCustomControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl))); } public Color HotColor { get { return (Color)GetValue(HotColorProperty); } set { SetValue(HotColorProperty, value); } } // Using a DependencyProperty as the backing store for HotColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty HotColorProperty = DependencyProperty.Register("HotColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(Colors.Aqua)); public Color ColdColor { get { return (Color)GetValue(ColdColorProperty); } set { SetValue(ColdColorProperty, value); } } // Using a DependencyProperty as the backing store for ColdColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ColdColorProperty = DependencyProperty.Register("ColdColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(Colors.Aqua)); } </code></pre>
 

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