2016-01-20 102 views
0

我在嘗試新的東西。我有幾個網格,當我嘗試抓住一個網格時,我想將其父母移到頂部網格上。目前,我在Grid_MouseDown方法的m_startOffset = new Vector(translate.X, translate.Y);上收到System.NullReferenceException。有人知道我應該如何解決這個問題嗎?WPF移動網格錯誤

UI:

<Grid x:Name="GridHost"> 
    <Grid x:Name="GRLogin" Margin="1401,292,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="501" d:IsHidden="True" Focusable="True"> 
     <Grid Height="30" VerticalAlignment="Top" Background="#FF1585B5" Margin="0" MouseLeftButtonUp="Grid_MouseUp" MouseLeftButtonDown="Grid_MouseDown" MouseMove="Grid_MouseMove" > 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Button x:Name="GRLoginClose" Content="X" Background="{x:Null}" Foreground="White" FontWeight="Bold" OpacityMask="Black" Margin="470,0,0,0" Width="30" Height="30" Click="Close"/> 
      <Button Content="-" Background="{x:Null}" Foreground="White" FontWeight="Bold" OpacityMask="Black" Margin="0,0,30,0" Width="30" Height="30" HorizontalAlignment="Right"/> 
      <Label Content="Login" HorizontalAlignment="Center" Margin="0" FontWeight="Bold"/> 
     </Grid> 
     <Grid HorizontalAlignment="Left" Height="250" Margin="0,30,0,0" VerticalAlignment="Top" Width="500" Background="#FF262626"> 
      <PasswordBox HorizontalAlignment="Center" Margin="206,130,120,94" VerticalAlignment="Center" Width="174"/> 
      <Label Content="Password" HorizontalAlignment="Center" Margin="122,130,308,94" VerticalAlignment="Center" RenderTransformOrigin="-1.737,0.462" Width="70"/> 
      <Label Content="Username" HorizontalAlignment="Center" Margin="122,99,308,125" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Width="70"/> 
      <TextBox HorizontalAlignment="Center" Height="23" Margin="206,99,120,125" TextWrapping="Wrap" VerticalAlignment="Center" Width="174" RenderTransformOrigin="0.467,-0.346"/> 
      <Button Content="Login" HorizontalAlignment="Left" VerticalAlignment="Top" Width="258" Margin="122,180,0,0" Height="40" Background="#FF1585B5" Foreground="White" FontWeight="Bold"/> 
     </Grid> 
    </Grid> 

移動代碼:

private void Grid_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkElement element = ((Grid)sender).Parent as Grid; 
     TranslateTransform translate = element.RenderTransform as TranslateTransform; 

     m_start = e.GetPosition(GridHost); 
     m_startOffset = new Vector(translate.X, translate.Y); 
     element.CaptureMouse(); 
    } 

    private void Grid_MouseMove(object sender, MouseEventArgs e) 
    { 
     FrameworkElement element = ((Grid)sender).Parent as Grid; 
     TranslateTransform translate = element.RenderTransform as TranslateTransform; 

     if (element.IsMouseCaptured) 
     { 
      Vector offset = Point.Subtract(e.GetPosition(GridHost), m_start); 

      translate.X = m_startOffset.X + offset.X; 
      translate.Y = m_startOffset.Y + offset.Y; 
     } 
    } 

    private void Grid_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkElement element = ((Grid)sender).Parent as Grid; 
     element.ReleaseMouseCapture(); 
    } 

回答

0

聲明父網格的的RenderTransform在XAML:

<Grid x:Name="GRLogin" Margin="1401,292,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="501" d:IsHidden="True" Focusable="True"> 
    <Grid.RenderTransform> 
     <TranslateTransform/> 
    </Grid.RenderTransform> 
    ... 
</Grid> 

你也可以檢查空了的RenderTransform,其中你設定它的情況:element.RenderTransform = new TranslateTransform();

+0

謝謝,出於某種奇怪的原因,當我這樣做時,它根本不會做任何事情。它看起來像程序變得沒有反應,因爲它不會對任何其他輸入做出反應。 – djkevino