【问题标题】:'Foreground' property does not point to a DependencyObject in path '(Foreground).(0)'“前景”属性未指向路径“(前景)。(0)”中的 DependencyObject
【发布时间】:2015-10-06 14:46:36
【问题描述】:

我有一个 WPF 画布项目,我从工具箱中将对象拖放到画布上。根据某些数据,其中一些对象应该闪烁或闪烁。我得到一个未处理的异常:Cannot animate '(Foreground).(0)' on an immutable object instance.. 以下是我的代码。有人建议使用 (Foreground).(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>

【问题讨论】:

  • 闪存? '其中一些对象应该闪烁'是什么意思。这些对象会包含 Flash 电影吗?还是它们会像发光等一样看起来不同。
  • 请贴出相关代码
  • @user3798700 我明白你的意思,将发布答案,正在努力。
  • @AnjumSKhan 我用更新的代码和错误消息修改了我的原始问题。

标签: c# wpf


【解决方案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="Label",并且运行良好。我使用自己的 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,可以说更多。

【讨论】:

  • 仍然出现相同的错误 - 无法在不可变对象实例上为“(前景)。(0)”设置动画。我在样式之前绑定了背景色和前景色。你认为这是导致它不起作用的原因吗?
  • @user3798700 你在吗?
  • 我尝试使用转换器,它给出了同样的错误。然后我删除了样式之外的绑定,它仍然给了我同样的错误。此时,我可能只是编写一个计时器线程,使其可见和不可见以模拟闪烁。
  • @user3798700 可能是您在 dropbox.com 上发布了一个可行的示例,或者我可以使用团队查看器查看
  • 这里是保管箱链接。 dropbox.com/s/kjr7wvdi7mnyaud/DiagramDesignerMVVMOct7.zip?dl=0 。有问题的视图模型是 SingleValueControlViewModel ,数据模板的 xaml 是 SingleValueControlDataTemplate 。如果您有任何问题,请告诉我。我剥离了大部分功能以保持其简单的 UI。
【解决方案2】:

这对我有用:

<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>

【讨论】:

  • 我尝试了上面的代码,但得到错误 - 无法在不可变对象实例上为“(前景)。(0)”设置动画。我还更新了 xaml,以便 storyboard.targetproperty="(Foreground).(SolidColorBrush.Color)" 但我仍然得到同样的错误。
  • 这是在我的数据模板中。当我粘贴您的代码时,它说属性 Content 设置了多次。我将编辑我的原始帖子以包含完整的 xaml。我很抱歉没有把完整的代码放在那里。感谢您迄今为止的所有帮助。
  • 可能是复制和粘贴错误。只需用我回答中的样式替换您的样式即可。
  • 同样的错误 - 无法在不可变对象实例上为“Foreground.Color”设置动画。 :(
猜你喜欢
  • 2012-10-21
  • 2014-05-04
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 2012-03-10
  • 1970-01-01
相关资源
最近更新 更多