Search code examples
c#wpfprism

C# WPF PRISM - Make UserControl Draggable


I already found a solution (How to make a user control draggable on screen like a window), but it doesn't work the way i wanted. In this solution only the content of the UserControl is draggable, but i open my UserDialog with the ShowDialog method, so the UserControl is a seperate Dialog (https://prismlibrary.com/docs/wpf/dialog-service.html).

Is there any equivalent to the DragMove() method for the UserControl?


Solution

  • I don't know if I understood correctly, but you want a behavior that when a user control is dragged you want the parent window to be dragged. So here it is :

    public class DragParentWindowBehavior : Behavior<UserControl>
        {
            private Window parentWindow;
    
            protected override void OnAttached()
            {
                AssociatedObject.Loaded += AssociatedObject_Loaded;
                AssociatedObject.Unloaded += AssociatedObject_Unloaded;
                AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
            }
    
            private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
            {
                parentWindow = null;
            }
    
            private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
            {
               //We get the parent window here. because the behavior is attached to the view way before it is shown.
                parentWindow = Window.GetWindow(AssociatedObject);
            }
    
            private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                parentWindow?.DragMove();
            }
    
            protected override void OnDetaching()
            {
                AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
                AssociatedObject.Loaded -= AssociatedObject_Loaded;
                AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
    
                parentWindow = null;
            }
        }
    

    This is how you use it in XAML :

    <UserControl x:Class="Prism1.Modules.ModuleName.Views.DraggableControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Prism1.Modules.ModuleName.Views" 
                 xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors" 
                 xmlns:mvvm="clr-namespace:Prism1.Core.Mvvm;assembly=Prism1.Core"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <Behaviors:Interaction.Behaviors>
            <mvvm:DragParentWindowBehavior></mvvm:DragParentWindowBehavior>
        </Behaviors:Interaction.Behaviors>
        <Grid x:Name="LayoutRoot" Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
    
            <TextBlock Text="{Binding Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" TextWrapping="Wrap" />
            <Button Command="{Binding CloseDialogCommand}" CommandParameter="true" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Grid.Row="1" IsDefault="True" />
        </Grid>
    </UserControl>