English 中文(简体)
Cant drag and move a WPF Form
原标题:

I design a WPF form with Window Style=None. So I Cannot see the drag bar in the form. How can I move the Form with WindowStyle=None Property?

问题回答

I am using a main window to hold pages (creating a navigation style program), and in the code behind of my main window, I inserted this...

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonDown(e);

    // Begin dragging the window
    this.DragMove();
}

... and it works like a charm. This is with windowstyle=none. Its nice in the sense that you can click anywhere on the app and move it instead of just being limited to a top bar.

See this question.

Basically you use the Window.DragMove method for this.

In our application we have Windows with WindowStyle set to "none", we implemented the functionality to drag the Window, but only from the header rather than from any point in the Window. We did this by adding a Border as a header, then adding a Thumb to fill the entire Border. We then handle the DragDelta method on the Thumb in the code-behind for the Window.

<Border 
        Name="headerBorder" 
        Width="Auto" 
        Height="50" 
        VerticalAlignment="Top"
        CornerRadius="5,5,0,0" 
        DockPanel.Dock="Top" 
        Background="{StaticResource BackgroundBrush}" 
        BorderThickness="1,1,1,1"
        BorderBrush="{StaticResource BorderBrush}">
        <Grid>
            <Thumb 
                x:Name="headerThumb" 
                Opacity="0" 
                Background="{x:Null}" 
                Foreground="{x:Null}" 
                DragDelta="headerThumb_DragDelta"/>
        </Grid>
    </Border>

Then in the code-behind we have the following event handler...

private void headerThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
    Left = Left + e.HorizontalChange;
    Top = Top + e.VerticalChange;
}

I don t know if this is any better than the other method, it s just the way we did it.

either inside the windows on load function or inside the grid s on load function use a deligate to trigger the DragMove() method on Mouse Click

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
            this.MouseLeftButtonDown += delegate{DragMove();};
}

If you simply add this.DragMove(); and you are using Bing Maps, then you will get some frustrating behavior when trying to pan the map.

Using TabbyCool s answer was a good solution however, you can not drag the window against the top of the screen to maximise it.

My solution was just checking that the position.Y of my click relative to my top bar grid was less than a suitable amount.

 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        Point pt = e.GetPosition(topBar);

        Debug.WriteLine(pt.Y);

        if (pt.Y < topBar.ActualHeight)
        {
            DragMove();
        }
    }




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签