简体   繁体   中英

WPF Move grid Error

I'm trying something new. I've got a few grids, and when I try to grab a grid i want to move its parent around my top grid. At the moment I get a System.NullReferenceException on m_startOffset = new Vector(translate.X, translate.Y); in the Grid_MouseDown method. Does someone know how i should tackle this problem?

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>

Movement code:

    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();
    }

Declare the parent Grid's RenderTransform in the 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>

You could also check the RenderTransform for null, in which case you set it: element.RenderTransform = new TranslateTransform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM