question

Sarah-3412 avatar image
0 Votes"
Sarah-3412 asked HuiLiu-MSFT answered

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

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-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@Sarah-3412. What effect do you want to achieve? Could you show the existing complete code of your problem?

0 Votes 0 ·
Sarah-3412 avatar image
0 Votes"
Sarah-3412 answered

Hi @HuiLiu-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>
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered

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 to enable e-mail notifications if you want to receive the related email notification for this thread. 

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.