WPF中的动画 - 在'System.Windows.Controls.ControlTemplate'的名称范围内找不到名称

时间:2011-12-04 12:34:15

标签: wpf styling

在更改标签时试图用一些动画制作一个tabcontrol,但它一直让我感到悲伤并且拒绝让我把动画放在任何有用的地方,除非它与控件本身在同一个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'名称

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

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

我在网上搜索了很多,但没有找到合适的答案......四天后,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>