Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>DynamicResources unfortunately don't exist in Silverlight.</p> <p>Some people use Bindings.</p> <p>There's a sneaky way to simulate the experience within a single UserControl that you might want to try.</p> <p>All that's happening in the code below is that the Storyboard animates a single rectangle's Fill, which is then bound to the other Fill's in the UserControl by using Element binding. The source rectangle doesn't need to be visible for this to work.</p> <pre><code>&lt;UserControl x:Class="TestSilverlightStuff.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestSilverlightStuff" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"&gt; &lt;UserControl.Resources&gt; &lt;SolidColorBrush x:Key="Background" Color="Black" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="MyStates"&gt; &lt;VisualState x:Name="Normal"/&gt; &lt;VisualState x:Name="Red"&gt; &lt;Storyboard&gt; &lt;ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="trickyRectangle" d:IsOptimized="True"/&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;/VisualStateGroup&gt; &lt;/VisualStateManager.VisualStateGroups&gt; &lt;Rectangle Fill="Black" x:Name="trickyRectangle" Visibility="Collapsed" /&gt; &lt;Border Background="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Red" BorderThickness="1"/&gt; &lt;Rectangle Fill="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Bottom"/&gt; &lt;Button Content="Button" Height="57" HorizontalAlignment="Left" Margin="12,231,0,0" Name="button1" VerticalAlignment="Top" Width="153" Click="button1_Click" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Here's the C# button click code:</p> <pre><code>private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { VisualStateManager.GoToState(this, "Red", true); } </code></pre> <p>It's not as elegant as a DynamicResource, but it works in some cases. </p>
 

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