WPF 移动网格错误

本文关键字:错误 网格 移动网 移动 WPF | 更新日期: 2023-09-27 18:31:12

我正在尝试新的东西。我有几个网格,当我尝试抓取一个网格时,我想在我的顶部网格周围移动它的父网格。目前,我在Grid_MouseDown方法中得到一个关于m_startOffset = new Vector(translate.X, translate.Y);的System.NullReferenceException。有人知道我应该如何解决这个问题吗?

用户界面:

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

WPF 移动网格错误

在 XAML 中声明父网格的 RenderTransform:

<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 是否为 null,在这种情况下,您可以设置它:element.RenderTransform = new TranslateTransform();