How to add property MouseDown to existing Attached Property (MVVM)

Sarah 186 Reputation points
2022-04-24T11:59:07.717+00:00

Hi, I use the following Attached Property for maximize and minimize the window. Now I want to use the property "MouseDown". Can someone show me how to add this property to the existing Attached Property?

 public static readonly DependencyProperty AttPropProperty =
     DependencyProperty.RegisterAttached("AttProp", typeof(bool), typeof(ViewModel), new PropertyMetadata(false, OnPropChanged));
 public static bool GetAttProp(DependencyObject obj) => (bool)obj.GetValue(AttPropProperty);
 public static void SetAttProp(DependencyObject obj, bool par) => obj.SetValue(AttPropProperty, par);
 private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
   Window w = d as Window;
   if (w == null) return;
   w.Loaded += (s, mbe) => ((ViewModel)(w.DataContext)).Wnd = w;
 }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sarah 186 Reputation points
    2022-04-26T07:03:54.593+00:00

    Hi @Hui Liu-MSFT , The following is the XAML code. I use WindowStyle="None. I want that the window can be moved with the mouse.

    <Window x:Class="Win.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:Win"  
            xmlns:vm="clr-namespace:Win.ViewModels.MainWindow"  
            mc:Ignorable="d"  
            Height="450"   
            Width="600"   
            WindowStartupLocation="CenterScreen"   
            Background="White"  
            WindowStyle="None"  
            vm:MainWindowViewModel.AttProp="True">  
          
        <Window.DataContext>  
            <vm:MainWindowViewModel/>  
        </Window.DataContext>  
          
        <Grid>  
            <!--Definition of 3 rows-->  
            <Grid.RowDefinitions >  
                <RowDefinition Height="70"/>  
                <RowDefinition Height="130"/>  
                <RowDefinition Height="*"/>  
            </Grid.RowDefinitions>  
      
            <Button Grid.Row="0"  
                    Margin="0,5,5,0"  
                    Command="{Binding Cmd}"  
                    CommandParameter="Close"/>  
            <Button Grid.Row="0"  
                    Margin="0,5,30,0"  
                    Command="{Binding Cmd}"  
                    CommandParameter="Maximize"/>  
            <Button Grid.Row="0"  
                    Margin="0,5,55,0"  
                    Command="{Binding Cmd}"  
                    CommandParameter="Minimize"/>  
      
        </Grid>  
    </Window>  
    
    0 comments No comments

  2. Hui Liu-MSFT 40,786 Reputation points Microsoft Vendor
    2022-05-06T02:06:46.647+00:00

    If you try to move the window through MVVM, it's hard to achieve. It's pure UI logic, not part of the ViewModel. You could refer to the solution here.

    You can also see a solution for moving windows with WindowStyle="none" here.


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments