WPF故事板开始 - 错误

时间:2017-07-14 08:40:53

标签: c# wpf storyboard invalidoperationexception

我在第sb.Begin(leftMenu);行(sb是故事板)中收到以下错误:

  

System.InvalidOperationException:'无法解析属性路径'margin'中的所有属性引用。验证适用的对象是否支持属性。'

XAML代码:

<Window x:Class="_10KHours.sliding"
    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:_10KHours"
    mc:Ignorable="d"
    Title="sliding" Height="500" Width="500">
<Window.Resources>
    <Storyboard x:Key="showMenu">
        <ThicknessAnimation Storyboard.TargetProperty="margin" From="-150,0,0,0" To="0,0,0,0" DecelerationRatio="0.8" Duration="0:0:1"/>
    </Storyboard>
    <Storyboard x:Key="hideMenu">
        <ThicknessAnimation Storyboard.TargetProperty="margin" From="0,0,0,0" To="-150,0,0,0" DecelerationRatio="0.8" Duration="0:0:1"/>
    </Storyboard>
</Window.Resources>
<Grid>
    <StackPanel Panel.ZIndex="2" Name="leftMenu" Orientation="Horizontal" HorizontalAlignment="Left" Margin="-150,0,0,0">
        <Border BorderBrush="AliceBlue" BorderThickness="1" Width="150" Background="AliceBlue">

        </Border>
    </StackPanel>
    <Grid>
        <Button Name="btnHide" Width="50" Click="btnHide_Click" Content="Hide" Visibility="Hidden"/>
        <Button Name="btnShow" Width="50" Click="btnShow_Click" Content="Hide" Visibility="Visible"/>
    </Grid>
</Grid>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace _10KHours
{
    /// <summary>
    /// Interaction logic for sliding.xaml
    /// </summary>
    public partial class sliding : Window
    {
        public sliding()
        {
            InitializeComponent();
        }

        private void btnHide_Click(object sender, RoutedEventArgs e)
        {
            menuSlide("showMenu",btnHide,btnShow,leftMenu);
        }

        private void menuSlide(string p, Button btnHide, Button btnShow, StackPanel leftMenu)
        {
            //throw new NotImplementedException();
            Storyboard sb = FindResource(p) as Storyboard;
            sb.Begin(leftMenu);
            if(p.Contains("show"))
            {
                btnHide.Visibility = Visibility.Visible;
                btnShow.Visibility = Visibility.Hidden;
            }
            else
            {
                if (p.Contains("hide"))
                {
                    btnHide.Visibility = Visibility.Hidden;
                    btnShow.Visibility = Visibility.Visible;
                }
            }  
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            menuSlide("hideMenu", btnHide, btnShow, leftMenu);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您收到的

Error是因为margin不是有效的属性使用 - &gt;而是Margin

    <Window.Resources>
    <Storyboard x:Key="showMenu">
        <ThicknessAnimation Storyboard.TargetProperty="Margin" From="-150,0,0,0" To="0,0,0,0" DecelerationRatio="0.8" Duration="0:0:10"/>
    </Storyboard>
    <Storyboard x:Key="hideMenu">
        <ThicknessAnimation Storyboard.TargetProperty="Margin" From="0,0,0,0" To="-150,0,0,0" DecelerationRatio="0.8" Duration="0:0:10"/>
    </Storyboard>
</Window.Resources>
<Grid>
    <StackPanel Panel.ZIndex="2" Name="leftMenu" Orientation="Horizontal" HorizontalAlignment="Left" Background="Yellow" Margin="-150,0,0,0">
        <Border BorderBrush="AliceBlue" BorderThickness="1" Width="150" Background="AliceBlue">

        </Border>
    </StackPanel>
    <Grid>
        <Button Name="btnHide" Width="50" Click="btnHide_Click" Content="Hide" Visibility="Hidden"/>
        <Button Name="btnShow" Width="50" Click="btnShow_Click" Content="Hide" Visibility="Visible"/>
    </Grid>
</Grid>