我想知道是否有一種簡單的方法來修改不同VisualState之間控件的某種共享資源(即Brush)。例如,我想定義一個畫筆作爲邊框的背景和不同矩形的填充。在不同的VisualState中,我希望將此背景畫筆在一個位置(資源)中更改,並將其反映在使用該資源的所有元素中。在VisualState中修改Silverlight資源
我不確定是否真的可以通過名稱(而不是鍵)爲VisualState中Storyboard的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 StaticResources他們只加載一次,不能更改。我知道WPF有一些DynamicResources的概念。有沒有辦法在Silverlight中實現這種類型的行爲,而無需在所有元素中重新定義我的畫筆?
我最終公開了Brush屬性綁定到我的視圖模型來完成這個任務,但是我看到這個技巧如何工作,所以我將其標記爲答案。兩種方法都覺得有點骯髒,但這對你來說是銀光。我只是想確保我沒有看到任何明顯的東西。謝謝! – 2010-07-31 19:05:57
+1用於綁定到您的視圖模型 – 2011-11-19 12:31:14