如何解决"可附加属性'触发器'未在类型' ControlTemplate'"?

时间:2015-11-24 13:38:55

标签: wpf xaml triggers storyboard controltemplate

我已按照此snippet向UpdateBtn添加了一个控件模板,但我收到一条错误消息,指出在ControlTemplate上找不到Triggers属性。我们的想法是将故事板绑定到按钮的IsEnabled属性。

Error   5   The attachable property 'Triggers' was not found in type 'ControlTemplate'. 

我研究了错误,似乎该属性是ControlTemplate的一部分,这是一个WPF应用程序而不是Windows。所以不确定为什么错误会在xaml设计器中显示出来。

任何人都可以就我在执行方面出现问题的地方提出建议吗?

按钮和相关ns的XAML如下:

<Window x:Class="MongoDBApp.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:email_validator="clr-namespace:MongoDBApp.Validator"
        Title="Orders Dashbord"
        Width="800"
        Height="500">

    <Grid>
        <TabControl>
            <TabItem Header="Customer">
                <Grid>

                    <Button x:Name="updateBtn"
                            Width="75"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            Command="{Binding Path=UpdateCommand}"
                            Content="Update">
                        <Button.ToolTip>
                            <ToolTip>
                                <StackPanel>
                                    <TextBlock>Updates customer record</TextBlock>
                                </StackPanel>
                            </ToolTip>
                        </Button.ToolTip>
                        <Button.Template>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </Trigger.EnterActions>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </Button.Template>
                    </Button>


                </Grid>
            </TabItem>           
        </TabControl>
    </Grid>

</Window>

1 个答案:

答案 0 :(得分:2)

您的XAML结构是

<Button.Template>
    <ControlTemplate.Triggers>
         ...
    </ControlTemplate.Triggers>
</Button.Template>

但你需要像

这样的东西
<Button.Template>
    <ControlTemplate TargetType="Button">
        <ControlTemplate.Triggers>
          ...
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Button.Template>