0

I want to move a borderless windows, and before I adopt the Prism framework, I'd do it as follows:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MouseDown += Window_MouseDown;
    }

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            DragMove();
        }
    }
}

but I don't know how to implement this while using Prism in MainWindowViewModel.cs (the view model), it seems the InvokeCommandAction can pass the event argument for an element like button or so, but it doesn't work for a window in my case.

Can anyone help me on this? Thanks in advance.

Winfred
  • 19
  • 6

2 Answers2

0

I don't know how to implement this while using Prism

I don't know what this is supposed to be exactly, but I'll assume it's how to call the view model when some event on the view happens:

The cleanest option is an attached behavior. Alternatively, you can use an InvokeCommandAction variant like DevExpress' EventToCommand that supports forwarding the parameter.

Haukinger
  • 10,420
  • 2
  • 15
  • 28
0

Well, finally I got the event fired, but it seems this approach contradict the concept of MVVM pattern, which requires that the view model should not know anything about nor has any dependency upon any view elements.

In my case, I can add the Interaction.Triggers to the Window and pass the MouseButton to the view model by usng Prism’s InvokeCommandAction, as follows:

<Window
        xmlns:prism="http://prismlibrary.com/"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
/>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown">
            <prism:InvokeCommandAction Command="{Binding WindowMouseCommand}" TriggerParameterPath="ChangedButton" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

and in the view model:

    public DelegateCommand<object> WindowMouseCommand { get; private set; }

...

    WindowMouseCommand = new DelegateCommand<object>(WindowMouse);

...

private void WindowMouse(object mouseButton)
{
    if (mouseButton is MouseButton m)
    {
        if (m == MouseButton.Left)
        {
            // DragMove();
        }
    }
}

if I want to call the .DragMove(), I need a reference of the Window... it's not the correct implementation of MVVM pattern.

So what is the best approach/practice for that?


I was suddenly enlightened when I saw this answer: https://stackoverflow.com/a/3426183/10958770

yes, moving a window is a pure UI logic, therefore it's not necessary to move it to a ViewModel... so let me just leave it in the view.

Winfred
  • 19
  • 6
  • 1
    Don't strictly follow the pattern. It's called design pattern for a reason and not theory or formula. If you strictly follows every details, you will lose your perspective. – phonemyatt Nov 11 '19 at 03:54
  • is it common that we, sometimes, have to deviate from the design pattern? @phonemyatt – Winfred Nov 11 '19 at 06:47
  • 1
    There is no right or wrong answer in programming. We learn about all this pattern to solve existing problems. at the end of the day, it's about how you speak the language to solve the problem. that's why everyone is unique. You might get some idea from this article: "http://neverindoubtnet.blogspot.com/2010/03/mvvm-josh-smiths-way.html", – phonemyatt Nov 11 '19 at 07:13
  • Thanks, that's really an inspiring article. – Winfred Nov 11 '19 at 09:49