当前位置: 代码迷 >> C# >> WPF,这个资源为啥不能引用
  详细解决方案

WPF,这个资源为啥不能引用

热度:500   发布时间:2016-05-05 05:32:51.0
WPF,这个资源为什么不能引用
本帖最后由 df43ttv 于 2014-12-19 13:45:26 编辑
一个自定义控件:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPF1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Name="PART_Border" Background="Red" Height="50" Width="50"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource aa}"/>
                            </Trigger.EnterActions>
                            <Setter TargetName="PART_Border" Property="Grid.Background" Value="{StaticResource brush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

一个应用程序资源:

<Application x:Class="WPF1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Storyboard x:Key="aa" TargetName="PART_Border" x:Shared="false">
            <DoubleAnimation Storyboard.TargetProperty="Height" To="200" Duration="0:0:1" />
        </Storyboard>
        <SolidColorBrush x:Key="brush" Color="Blue"/>
    </Application.Resources>
</Application>


<Window x:Class="WPF1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:CustomControl1/>
    </Grid>
</Window>


在自定义的控件模板中,有个一触发器,启动了一个动画,这个动画引用APP资源中的"aa"资源,可是,为什么报错呢?
提示无法找到名为"aa"的资源,这是为什么呢?而brush资源则是能正常引用的。
------解决思路----------------------
Storyboard 不是静态资源。

描述中: 有个一触发器,启动了一个动画。
1:有个一触发器??怎么没有trigger定义。
2: 要先搞清楚 在什么样的情况下 触发了动画????loaded?click?还是什么。但未见你代码中那个地方设置了。

------解决思路----------------------
你定义的资源字典怎么没引用,在window里引入资源字典看看

    <Window.Resources>
        <ResourceDictionary Source="xxxDictionary.xaml"></ResourceDictionary>
    </Window.Resources>
  相关解决方案