如何解决“在类型‘ControlTemplate’中找不到可附加属性‘触发器’”?

Bri*_*n J 1 wpf xaml triggers storyboard controltemplate

我已按照此代码片段将控件模板添加到 UpdateBtn,但收到一条错误消息,指出Triggers在 ControlTemplate 上未找到该属性。这个想法是将故事板绑定到IsEnabled按钮的属性。

Error   5   The attachable property 'Triggers' was not found in type 'ControlTemplate'. 
Run Code Online (Sandbox Code Playgroud)

我研究了该错误,看来该属性是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>
Run Code Online (Sandbox Code Playgroud)

Fra*_*tyx 5

您的 XAML 结构是

<Button.Template>
    <ControlTemplate.Triggers>
         ...
    </ControlTemplate.Triggers>
</Button.Template>
Run Code Online (Sandbox Code Playgroud)

但你需要类似的东西

<Button.Template>
    <ControlTemplate TargetType="Button">
        <ControlTemplate.Triggers>
          ...
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Button.Template>
Run Code Online (Sandbox Code Playgroud)