2010-07-30 69 views
1

我想知道是否有一種簡單的方法來修改不同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中實現這種類型的行爲,而無需在所有元素中重新定義我的畫筆?

回答

0

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

有些人使用綁定。

有一種偷偷摸摸的方式來模擬您可能想要嘗試的單個UserControl內的體驗。

以下代碼中發生的所有情況是,Storyboard通過使用Element綁定將單個矩形的填充動畫化,然後將其綁定到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> 

這裏是C#按鈕,單擊代碼:

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

它不是作爲一個DynamicResource優雅,但在某些情況下工作。

+1

我最終公開了Brush屬性綁定到我的視圖模型來完成這個任務,但是我看到這個技巧如何工作,所以我將其標記爲答案。兩種方法都覺得有點骯髒,但這對你來說是銀光。我只是想確保我沒有看到任何明顯的東西。謝謝! – 2010-07-31 19:05:57

+0

+1用於綁定到您的視圖模型 – 2011-11-19 12:31:14