'Foreground'属性不指向路径'(前景)中的DependencyObject。(0)'

时间:2015-10-06 14:46:37

标签: c# wpf

我有一个WPF画布项目,我从工具箱中拖放画布上的对象。根据某些数据,其中一些对象应闪烁或闪烁。我得到一个未处理的异常:无法在不可变对象实例上设置'(前景)。(0)'动画。以下是我的代码。有人建议使用(前景)。(SolidColorBrush.Color)我在我的标记中更改了它,但它似乎没有解决它。

<!-- DataTemplate for DesignerCanvas look and feel -->
<DataTemplate DataType="{x:Type viewModels:SingleValueControlViewModel}">
    <Grid>

        <Label Name="label" Content="{Binding TagValue}" IsHitTestVisible="False" Height="{Binding ItemHeight}" Width="{Binding ItemWidth}" 
          Background="{Binding BackColor}"     
          Foreground="{Binding ForeColor}"
          BorderBrush="{Binding BorderColor}"
          BorderThickness="{Binding StyleProperties.BorderWidth}"
          FontFamily="{Binding StyleProperties.Font.FontFamily}"
          FontSize = "{Binding StyleProperties.Font.Size}"
          FontStyle="{Binding StyleProperties.Font.Style}"
          HorizontalContentAlignment="{Binding TextAlign}" >
<Label.Style>
              <Style>
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding StyleProperties.FlashEnable}" Value="true">
                          <Setter Property="Label.Background" Value="Black"></Setter>
                          <Setter Property="Label.Foreground" Value="Red"></Setter>
                          <DataTrigger.EnterActions>
                              <BeginStoryboard>
                                  <Storyboard RepeatBehavior="Forever">
                                        <ColorAnimation
                                          Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
                                          Duration="00:00:00:01"
                                          From="Black" To="Red">
                                        </ColorAnimation>
                                  </Storyboard>
                              </BeginStoryboard>
                          </DataTrigger.EnterActions>
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
          </Label.Style>

    </Grid>
</DataTemplate>

2 个答案:

答案 0 :(得分:1)

/ *检查用户代码后的最终答案 * /

将其添加到您的DiagramControl.xaml

<DataTrigger Binding="{Binding IsSelected}" Value="True">                                                         
    <Setter Property="Opacity" Value="1"/>
    <DataTrigger.EnterActions>
         <BeginStoryboard>
            <Storyboard RepeatBehavior="Forever">
                 <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To=" 0.1" Duration="00:00:0.3"/>
            </Storyboard>
         </BeginStoryboard>
    </DataTrigger.EnterActions>                                                
</DataTrigger>

/ *由于用户仍然无法运行动画并在评论中报告错误,因此进行全新更新 * /

您必须绑定Background,Foreground属性,并记住Brush对象是不可变的。有一个解决方法,如以下msdn链接:

中所述

immutable instance animation error

debugging animations

/ *用户使用现有XAML代码* /

更新了他的问题后发布了新答案

我已经使用了您的代码,如下所示,添加了一个TargetType =&#34; Label&#34;它工作得很好。我使用自己的StyleProperties.FlashEnable绑定让DataTrigger工作。

这是一方面。另一方面:当您将项目拖动到Canvas时,您正在动态执行所有操作。为此,您需要在代码中应用样式/触发器。

<Grid>
<Label Content="Hi ! I am added dynamically" TextBlock.FontSize="45">
  <Label.Style>
    <Style TargetType="Label">
      <Style.Triggers>
        <DataTrigger Binding="{Binding StyleProperties.FlashEnable, Mode=OneWay}" Value="true">
         <Setter Property="Label.Background" Value="Black"></Setter>
         <Setter Property="Label.Foreground" Value="Red"></Setter>
         <DataTrigger.EnterActions>
            <BeginStoryboard>
               <Storyboard RepeatBehavior="Forever">
                 <ColorAnimation 
                     Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
                     Duration="00:00:00:01"
                     From="Black" To="Red">
                  </ColorAnimation>
                </Storyboard>
             </BeginStoryboard>
          </DataTrigger.EnterActions>
      </DataTrigger>                        
      </Style.Triggers>
   </Style>
  </Label.Style>
</Label>
</Grid>

/ *在用户更新他的问题之前发布旧答案* / 要显示对象闪烁,您必须更改其Foreground属性。而且这个前景颜色必须来自某个变量。对于绑定,您必须使用依赖项属性,或者包含属性/变量的类必须实现INotifyPropertyChanged,以便您的属性引发PropertyChanged事件。

如果您使用动画,还应该为Foreground提供一些初始值。

您也可能使用DynamicResource而不是StaticResource。

如果您发布一些XAML,可以说更多。

答案 1 :(得分:0)

这对我有用:

<Style TargetType="Label">
    <Style.Triggers>
        <DataTrigger Binding="{Binding FlashEnable}" Value="True">
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Foreground" Value="Red"/>
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever">
                        <ColorAnimation
                            Storyboard.TargetProperty="Foreground.Color"
                            Duration="0:0:1" From="Black" To="Red">
                        </ColorAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

您还可以在Foreground属性的样式设置器中显式设置SolidColorBrush:

<Style TargetType="Label">
    <Style.Triggers>
        <DataTrigger Binding="{Binding FlashEnable}" Value="True">
            <Setter Property="Background" Value="Black"/>
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Red"/>
                    </Setter.Value>
                </Setter>
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever">
                        <ColorAnimation
                            Storyboard.TargetProperty="Foreground.Color"
                            Duration="0:0:1" From="Black" To="Red">
                        </ColorAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>