我想知道是否有一种简单的方法可以修改不同 VisualStates 之间控件的某种共享资源(即画笔)。例如,我想定义一个画笔作为边框的背景和不同矩形的填充。在不同的 VisualState 中,我想在一个地方(资源)更改此背景画笔,并将其反映在使用该资源的所有元素中。
我不确定是否真的可以通过名称(而不是键)为 VisualState 中情节提要的 TargetName 引用资源。
这是我在 XAML 中尝试做的一个简化示例:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication.MainPage"
Width="200" Height="200">
<UserControl.Resources>
<SolidColorBrush x:Name="Background" x:Key="Background" Color="Black" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Red">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Color)">
<EasingColorKeyFrame KeyTime="00:00:00" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border Background="{StaticResource Background}" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Red" BorderThickness="1"/>
<Rectangle Fill="{StaticResource Background}" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>
我有一种感觉,因为这些是 Silverlight 中的静态资源,它们只加载一次并且无法更改。我知道 WPF 有一些动态资源的概念。有什么方法可以在 Silverlight 中实现这种类型的行为,而无需在所有元素中重新定义我的画笔?