0

对于我的生活,我无法弄清楚为什么下面的简单设置不起作用。我只希望边框在加载后放大(“放大”),放大功能包含在资源样式中。但是当下面的 XAML 运行时,边框只是以 100% 的比例加载,没有动画发生。您的帮助将不胜感激!

    <Style x:Key="SZoomIn" TargetType="FrameworkElement">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <ScaleTransform/>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)">
                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                            <LinearDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleY)">
                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                            <LinearDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>            
    </Style>
    <Style x:Key="SBorder" BasedOn="{StaticResource SZoomIn}" TargetType="Border">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    </Style>

    <!-- ....... -->

    <Border Style="{StaticResource SBorder}"/>

编辑:我没有在上面显示完整的标记,以便让事情保持简单,但我的完整标记在下面。请注意,由于边框内有按钮,因此边框在运行时确实具有高度/宽度。

<Window x:Name="StartWindow" x:Class="MEACruncher.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MEACruncher"
    mc:Ignorable="d"
    Title="MEA Cruncher" Height="393" Width="659" Margin="0" WindowState="Maximized">
<Window.Background>
    <ImageBrush ImageSource="Resources/NeuronBackground.jpg" Stretch="UniformToFill"/>
</Window.Background>
<Window.Resources>
    <SolidColorBrush x:Key="BabyBlue">#FF4D7EB8</SolidColorBrush>
    <SolidColorBrush x:Key="BlackBkgrd">#B2000000</SolidColorBrush>
    <Style x:Key="SBtn" TargetType="Button">
        <Setter Property="Margin" Value="0,5"/>
        <Setter Property="Background" Value="{StaticResource BabyBlue}"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
        <Setter Property="Padding" Value="10,3"/>
        <Setter Property="FontSize" Value="24"/>
    </Style>
    <Style x:Key="STopBtn" BasedOn="{StaticResource SBtn}" TargetType="Button">
        <Setter Property="Margin" Value="0,0,0,5"/>
    </Style>
    <Style x:Key="SBottomBtn" BasedOn="{StaticResource SBtn}" TargetType="Button">
        <Setter Property="Margin" Value="0,5,0,0"/>
    </Style>
    <Style x:Key="SZoomIn" TargetType="FrameworkElement">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <ScaleTransform/>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)">
                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                            <LinearDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleY)">
                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                            <LinearDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>            
    </Style>
    <Style x:Key="SBorder" BasedOn="{StaticResource SZoomIn}" TargetType="Border">
        <Setter Property="BorderBrush" Value="{StaticResource BabyBlue}"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="Background" Value="{StaticResource BlackBkgrd}"/>
        <Setter Property="CornerRadius" Value="5"/>
        <Setter Property="Padding" Value="10"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    </Style>
    <Style x:Key="SStackPnl" TargetType="StackPanel">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>            
    </Style>
</Window.Resources>

<Border x:Name="MainBorder" Style="{StaticResource SBorder}">
    <StackPanel x:Name="MainStackPnl" Style="{StaticResource SStackPnl}">
        <Button x:Name="ViewProjectsBtn" Content="View Projects" Style="{StaticResource STopBtn}" Click="ViewProjectsBtn_Click"/>
        <Button x:Name="AboutBtn" Content="About" Style="{StaticResource SBtn}" Click="AboutBtn_Click"/>
        <Button x:Name="ExitBtn" Content="Exit" Style="{StaticResource SBottomBtn}" Click="ExitBtn_Click"/>
    </StackPanel>
</Border>

</Window>
4

2 回答 2

1

如果以上是您的全部内容,请XAML按以下方式更改您的代码:

<Border Style="{StaticResource SBorder}" Background="Red" Height="200" Width="200"/>

您在 Border 中设置了VerticalAlignmentand并没有设置 aby &所以它根本不会显示。要么给 Height & Width 一些值,要么设置 和到,你会看到它工作正常。如下:HorizontalAlignmentCenterHeightWidthVerticalAlignmentHorizontalAlignmentStretchAnimation

 <Style x:Key="SBorder" BasedOn="{StaticResource SZoomIn}" TargetType="Border">
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    </Style>

如果您有其他一些控制层次结构,那么更新有问题的,我会更新我的答案。

更新:

在您更新的标记中,边框将在渲染时具有Height& Width。但SZoomIn风格似乎与之前给出的 XAML 不同。您没有Storyboard.TargetProperty正确设置属性。您缺少RenderTransform其中的前缀。

所以解决方案是:

将您的Storyboardin latestXAMLStoryboardin first替换掉XAML

于 2016-02-20T21:00:50.430 回答
0

所以,正如评论中提到的,我在这里发布的代码实际上运行良好。问题在于我在原始帖子中遗漏的另一个标记元素我创建了一个继承自 ContentControl 的名为 DraggableArea 的类,并且这个元素包围了我的 Border(请参阅下面的 FULL 标记)。DraggableArea 使任何内容都可以用鼠标拖动。我把它省略了 b/c 我想每个人都会专注于此,而不是看周围的 XAML,我确信问题出在哪里。原来我错了,像往常一样,我的自定义代码导致了这个错误。不知道究竟为什么会发生这个错误,但你去了。感谢所有的帮助!

<local:DraggableArea>
    <Border x:Name="MainBorder" Style="{StaticResource SBorder}">
        <StackPanel x:Name="MainStackPnl" Style="{StaticResource SStackPnl}">
            <Button x:Name="ViewProjectsBtn" Content="View Projects" Style="{StaticResource STopBtn}" Click="ViewProjectsBtn_Click"/>
            <Button x:Name="AboutBtn" Content="About" Style="{StaticResource SBtn}" Click="AboutBtn_Click"/>
            <Button x:Name="ExitBtn" Content="Exit" Style="{StaticResource SBottomBtn}" Click="ExitBtn_Click"/>
        </StackPanel>
    </Border>
</local:DraggableArea>
于 2016-02-21T06:06:22.640 回答