2017-07-14 96 views
1

我得到在該行sb.Begin(leftMenu);以下錯誤(SB是一個故事板):WPF故事板開始 - 錯誤

System.InvalidOperationException:「無法解析的屬性路徑的所有屬性引用‘保證金’。驗證適用的對象是否支持這些屬性。'

的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); 
     } 
    } 
} 
+0

@LeiYang我怎麼能允許這樣做? –

回答

1

Error你得到是因爲margin是不是有效的屬性使用 - >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> 

+0

謝謝,它的工作! –

+0

很高興幫助:) – tabby