2012-03-17 69 views
3

好更改StackPanel的背景顏色,我試圖使用ColorAnimation改變一個StackPanel的背景顏色在一個DataTemplate:與ColorAnimation

<DataTemplate DataType="{x:Type logic:Sensor}"> 
     <StackPanel Name="SensorPanel" MouseDown="SensorPanel_MouseDown"> 
     </StackPanel> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding Status}" Value="0"> 
       <!--<Setter TargetName="SensorPanel" Property="Background" Value="LawnGreen" />--> 

       <DataTrigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
           Storyboard.TargetName="SensorPanel" 
           Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" 
           To="LawnGreen" Duration="0:0:0.25" AutoReverse="True" RepeatBehavior="4"> 
          </ColorAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 

      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

沒有編譯時錯誤。但是當我運行這個時會拋出一個InvalidOperationException異常:「'Background''屬性沒有指向路徑'(0)。(1)'中的DependencyObject'。」

什麼? :d

+0

[鏈接](HTTP:/ /stackoverflow.com/questions/17399210/background-property-does-not-point-to-a-dependencyobject-in-path-0-1) 您可能會看到這個鏈接,因爲它類似於您的問題。 – 2013-08-05 08:41:57

回答

17

你的代碼工作完美的我。我只做了小修改。

<DataTemplate DataType="{x:Type Model:Sensor}"> 
     <StackPanel Name="SensorPanel" Background="LightBlue" Width="100" Margin="5"> 
      <TextBlock Text="{Binding Name}"/> 
      <ToggleButton Margin="2" IsChecked="{Binding IsChecked}" Content="Set status=0" /> 
     </StackPanel> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding Status}" Value="0"> 
       <!--<Setter TargetName="SensorPanel" Property="Background" Value="LawnGreen" />--> 

       <DataTrigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
          Storyboard.TargetName="SensorPanel" 
          Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" 
          To="LawnGreen" Duration="0:0:0.25" AutoReverse="True" RepeatBehavior="4"> 
          </ColorAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 

      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

    <ListBox ItemsSource="{Binding Sensors}" /> 
+0

謝謝,但它是否應該永久重複?因爲它。 :( – liranxs 2012-03-17 15:37:42

+4

RepeatBehaviour =「4倍」將修復它指定一個號碼被解釋爲時間跨度等什麼你有辦法'04:00:00' 或‘4小時’ – Phil 2012-03-17 15:42:27

10

對於文檔目的:

這是一個有點難以用(Panel.Background).(SolidColorBrush.Color)。真正的問題是,ColorAnimation只能爲Color財產,而不是Brush。對我來說,這是招:

定義你的面板刷...

<StackPanel Name="SensorPanel" MouseDown="SensorPanel_MouseDown"> 
    <StackPanel.Background> 
     <SolidColorBrush Color="White" x:Name="PanelColor"/> 
    </StackPanel.Background> 
</StackPanel> 

...然後動畫的SolidColorBrushColor屬性來代替:

<ColorAnimation 
    Storyboard.TargetName="PanelColor" 
    Storyboard.TargetProperty="Color" 
    To="LawnGreen" Duration="0:0:0.25" AutoReverse="True" RepeatBehavior="4"> 
+0

應該是公認的答案。 – 2017-11-23 16:03:24

相關問題