1

我正在设置DataGridRow BackgroundColor为绿色或红色,并根据LogError值触发。

我想用透明度为新添加的行设置动画。

这工作正常:

From="Transparent" To="Red"

但我想要颜色去的是当前的颜色设置与样式。它并不总是红色,它也可能是绿色。

这不起作用:

From="Transparent" To="{TemplateBinding DataGridRow.Background}"

或者

From="Transparent" To="{Binding RelativeSource={RelativeSource Self}, Path=Backgound}"

代码:

<DataGrid.Resources>
  <Style TargetType="{x:Type DataGridRow}">
    <Setter Property = "Background" Value="LimeGreen"/>
    <Style.Triggers>
      <DataTrigger Binding="{Binding LogMessage}" Value="Exception has occured">
        <Setter Property = "Background" Value="Red"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</DataGrid.Resources>          
<DataGrid.RowStyle>
  <Style TargetType="DataGridRow">
    <Style.Triggers>
      <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation
                Storyboard.TargetProperty="(DataGridRow.Background).(SolidColorBrush.Color)" 
                Duration="00:00:03" 
                From="Transparent"
                To="{TemplateBinding DataGridRow.Background}"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Style.Triggers>
  </Style>
</DataGrid.RowStyle>

错误信息:Cannot freeze this Storyboard timeline tree for use across threads.

4

2 回答 2

5

您的 XAML 代码中几乎没有问题。

首先,您在 DataGrid 的资源部分指定了默认样式,然后提供您自己的样式,该样式将覆盖默认样式。您应该定义新样式并将其BasedOnDP 设置为引用默认样式。但在你的情况下,我没有看到any use of defining separate style just for trigger

其次,您希望您的动画从Transparent样式中选择的颜色可以由 LimeGreen 或 Red 取决于触发器。因此,您不应To在动画中设置值,因为它会自动拾取。

这将按您的意愿工作-

   <DataGrid>
      <DataGrid.Resources>
          <Style TargetType="{x:Type DataGridRow}">
              <Setter Property ="Background" Value="LimeGreen"/>
              <Style.Triggers>
                 <DataTrigger Binding="{Binding LogMessage}"
                                        Value="Exception has occured">
                     <Setter Property = "Background" Value="Red"/>
                 </DataTrigger>
                 <EventTrigger RoutedEvent="Loaded">
                     <BeginStoryboard>
                         <Storyboard>
                             <ColorAnimation Storyboard.TargetProperty=
                              "(DataGridRow.Background).(SolidColorBrush.Color)" 
                                             Duration="00:00:03" 
                                             From="Transparent"/>
                         </Storyboard>
                     </BeginStoryboard>
                 </EventTrigger>
               </Style.Triggers>
            </Style>
        </DataGrid.Resources>
   </DataGrid>
于 2013-08-24T14:27:44.123 回答
1

@Den 在制作动画之前,您必须确定标准颜色。这是我为行和单元格设置动画的样式。

            <Style TargetType="{x:Type DataGridRow}">
            <Style.Setters>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Background" Value="Transparent"/>
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="#FFF37C21"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="(DataGridRow.Background).(SolidColorBrush.Color)" Duration="00:00:0.2" To="#FFF37C21"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="(DataGridRow.Background).(SolidColorBrush.Color)" Duration="00:00:0.2" To="Transparent"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>


            <Style TargetType="{x:Type DataGridCell}">
            <Style.Setters>
                <Setter Property="FontWeight" Value="Light"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid x:Name="gridCell" Background="#00FFFFFF">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="3"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"/>
                                </Grid.RowDefinitions>

                                <ContentPresenter 
                                        Grid.Column="1" 
                                        HorizontalAlignment="Left" 
                                        VerticalAlignment="Center"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="Black"/>
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.FontWeight)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:0.2">
                                        <DiscreteObjectKeyFrame.Value>
                                            <FontWeight>Bold</FontWeight>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.FontWeight)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:0.2">
                                        <DiscreteObjectKeyFrame.Value>
                                            <FontWeight>Light</FontWeight>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Style.Resources>
</Style>
于 2016-05-18T06:35:39.197 回答