在VisualState中修改Silverlight资源
我想知道是否有一种简单的方法可以在不同的VisualState之间修改控件的某种共享资源(即画笔)。例如,我想定义一个笔刷,用作边框的背景和另一个矩形的填充。在不同的VisualState中,我希望在一个地方(资源)更改此背景笔刷,并将其反映在使用资源的所有元素中 对于VisualState中故事板的TargetName,我不确定是否可以通过名称(而不是键)引用资源 下面是我在XAML中尝试执行的一个简化示例:在VisualState中修改Silverlight资源,silverlight,visualstatemanager,brush,staticresource,Silverlight,Visualstatemanager,Brush,Staticresource,我想知道是否有一种简单的方法可以在不同的VisualState之间修改控件的某种共享资源(即画笔)。例如,我想定义一个笔刷,用作边框的背景和另一个矩形的填充。在不同的VisualState中,我希望在一个地方(资源)更改此背景笔刷,并将其反映在使用资源的所有元素中 对于VisualState中故事板的TargetName,我不确定是否可以通过名称(而不是键)引用资源 下面是我在XAML中尝试执行的一个简化示例: <UserControl xmlns="http://schemas.micr
<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中实现这种行为而不必在所有元素中重新定义笔刷?很遗憾,Silverlight中不存在动态资源 有些人使用绑定 有一种巧妙的方法可以在单个用户控件中模拟体验,您可能想尝试一下 下面代码中发生的所有事情是,故事板为单个矩形的填充设置动画,然后使用元素绑定将其绑定到UserControl中的其他填充。源矩形不需要是可见的,它就可以工作
<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">
<UserControl.Resources>
<SolidColorBrush 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>
<ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="trickyRectangle" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle Fill="Black" x:Name="trickyRectangle" Visibility="Collapsed" />
<Border Background="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Red" BorderThickness="1"/>
<Rectangle Fill="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<Button Content="Button" Height="57" HorizontalAlignment="Left" Margin="12,231,0,0" Name="button1" VerticalAlignment="Top" Width="153" Click="button1_Click" />
</Grid>
</UserControl>
它不像DynamicResource那样优雅,但在某些情况下可以工作 我最终在视图模型上公开了要绑定到的笔刷属性以完成此操作,但我看到了此技巧的工作原理,因此我将其标记为答案。两种方法都感觉有点脏,但这对你来说是silverlight。我只是想确保我没有忽略任何明显的东西。谢谢
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Red", true);
}