Анимация вещей в WPF - имя не может быть найдено в области имен System.Windows.Controls.ControlTemplate. - PullRequest
0 голосов
/ 04 декабря 2011

Я пытался создать 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>

Это приводит к тому, что имя 'TabControlContent' не может быть найдено в области имен 'System.Windows.Controls.ControlTemplate'

Я также пытался переместить анимацию в начало файла, что приводит к той же ошибке. Если я добавлю его по стилю, раскадровка не сможет его найти. Есть ли способ обойти это?

Ответы [ 2 ]

1 голос
/ 12 декабря 2016

Решение:

используйте 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" />
0 голосов
/ 19 октября 2015

Я много ищу в сети, но не нашел подходящего ответа ... и через четыре дня попробую 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>
...