1

我想知道是否有一种简单的方法可以修改不同 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 中实现这种类型的行为,而无需在所有元素中重新定义我的画笔?

4

1 回答 1

0

不幸的是,Silverlight 中不存在 DynamicResources。

有些人使用绑定。

有一种偷偷摸摸的方法可以模拟您可能想尝试的单个 UserControl 中的体验。

下面的代码中发生的所有事情是 Storyboard 为单个矩形的 Fill 设置动画,然后使用 Element 绑定将其绑定到 UserControl 中的其他 Fill。源矩形不需要可见就可以工作。

<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>

这是 C# 按钮单击代码:

private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
    VisualStateManager.GoToState(this, "Red", true);
}

它不像 DynamicResource 那样优雅,但在某些情况下它可以工作。

于 2010-07-31T01:51:22.963 回答