【问题标题】:Animating things in WPF - name cannot be found in the name scope of 'System.Windows.Controls.ControlTemplate'WPF 中的动画 - 在“System.Windows.Controls.ControlTemplate”的名称范围内找不到名称
【发布时间】:2022-04-13 21:01:07
【问题描述】:

在更改选项卡时尝试制作带有一些动画的选项卡控件,但它一直让我感到悲伤并拒绝让我将动画放在任何有用的地方,除非它与控件本身在同一个 XAML 窗口文件中(样式位于其他样式工作的 DLL 文件)。这是我的风格:

<Style x:Key="AnimatedTabControl" TargetType="{x:Type TabControl}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Background" Value="White" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">

                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border>
                        <TabPanel
                                IsItemsHost="True">
                        </TabPanel>
                    </Border>
                    <Border BorderThickness="0"
                                Grid.Row="1"
                                BorderBrush="White"
                                Background="White">
                        <ContentPresenter x:Name="TabControlContent" ContentSource="SelectedContent" Margin="0" />
                    </Border>
                </Grid>
                <ControlTemplate.Resources>
                    <Storyboard x:Key="TabSelectionChangedStoryboard">
                        <DoubleAnimation Storyboard.TargetName="TabControlContent"
                         Storyboard.TargetProperty="Opacity"
                         To="100"
                         From="0"
                         FillBehavior="HoldEnd"
                         Duration="0:0:30.0" />
                    </Storyboard>
                </ControlTemplate.Resources>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="SelectionChanged">
                        <BeginStoryboard Storyboard="{StaticResource TabSelectionChangedStoryboard}" />
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这导致在“System.Windows.Controls.ControlTemplate”的名称范围内找不到“TabControlContent”名称

我还尝试将动画移动到文件的开头,这会导致同样的错误。如果我把它放在 style 之后,storyboard 就找不到了。有没有办法解决这个问题?

【问题讨论】:

  • 我刚刚将您的 Xaml 导入到一个项目中,它未经修改就可以正常工作。事实上,这是一种非常好的风格。我正在使用带有 SP1 的 VS2010,并且没有收到任何警告或错误。
  • 它可能是我导入的样式/控件/实用程序库的一部分。如果我将此样式存储在程序的窗口中,它似乎可以工作,但如果我从 DLL 文件加载它,我会收到该错误。
  • 无论我移动了多少东西,在 DLL 中我都无法让它工作,但它可以在窗口 XAML 本身的 中声明的任何设置中工作。这是因为 WPF 如何处理它的线程还是什么?有人会认为,既然都在 中声明,它应该可以工作吗?谷歌搜索了更多,发现了一些其他在这里并不适用的解决方案(它不是用户控件)。是否可以将 存储在 中,然后将其放置在边框中应有的位置?如果是这样,你怎么做? :P

标签: wpf styling


【解决方案1】:

解决办法:

使用Storyboard.Target 而不是Storyboard.TargetName{Binding ElementName=TabControlContent 结合使用。

替换

<DoubleAnimation 
    Storyboard.TargetName="TabControlContent"
    Storyboard.TargetProperty="Opacity"
    To="100"
    From="0"
    FillBehavior="HoldEnd"
    Duration="0:0:30.0" />

<DoubleAnimation 
    Storyboard.Target="{Binding ElementName=TabControlContent}"
    Storyboard.TargetProperty="Opacity"
    To="100"
    From="0"
    FillBehavior="HoldEnd"
    Duration="0:0:30.0" />

【讨论】:

    【解决方案2】:

    我在网上搜索了很多但没有找到合适的答案...四天后尝试Finally以这种方式完成...

    <ControlTemplate x:Key="GeneralButton" TargetType="{x:Type Button}">
        <Grid>
            <Border Background="{StaticResource ButtonGeneral}" 
                                    VerticalAlignment="Stretch" CornerRadius="6" HorizontalAlignment="Stretch"/>
    
            <Border x:Name="BorderFocused" Opacity="0"  Background="{StaticResource ButtonFocused}" 
                                    VerticalAlignment="Stretch" CornerRadius="6" HorizontalAlignment="Stretch"/>
    
            <Border x:Name="BorderPressed"  Opacity="0" Background="Purple" 
                                    VerticalAlignment="Stretch" CornerRadius="6" HorizontalAlignment="Stretch"/>
    
            <Border x:Name="BorderDisabled"  Opacity="0" Background="{StaticResource ButtonDisabled}" 
                                    VerticalAlignment="Stretch" CornerRadius="6" HorizontalAlignment="Stretch"/>
    
            <ContentPresenter VerticalAlignment="Center" 
                                              HorizontalAlignment="Center" x:Name="MainContent" Margin="20,5"  >
                <TextElement.Foreground>
                    <SolidColorBrush Color="White"></SolidColorBrush>
                </TextElement.Foreground>
                <TextElement.FontSize>
                    16
                </TextElement.FontSize>
            </ContentPresenter>
        </Grid>
    
        <ControlTemplate.Triggers>
            <Trigger Property="IsKeyboardFocused" Value="true">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderFocused" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.01"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderFocused" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.4"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderFocused" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.01"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderFocused" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.4"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.01"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.5"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderDisabled" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="BorderDisabled" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
    
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    【讨论】:

      【解决方案3】:

      我使用“VisualStateManager”解决了这个问题,在以下链接中,您将找到“VisualStateManager”和“触发器”之间差异的简要说明。

      https://stackoverflow.com/a/41030110/13037386

      这显示了图形状态如何与触发器分离。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多