1

I have a xaml window and on the StateChanged event of the window I have to execute a piece of code. I have to follow MVVM. I binded the StateChanged property to an ICommand? It doesn't work.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="DummyApp"
x:Name="Window"
Title="Dummy App"
Width="{Binding WindowWidth,Mode=OneWayToSource}" Height="{Binding WindowHeight}" ResizeMode="CanMinimize" Icon="Logo.png" SizeToContent="WidthAndHeight"  WindowStartupLocation="CenterScreen" WindowState="{Binding CurrentWindowState, Mode=TwoWay}"
ShowInTaskbar="{Binding ShowInTaskBar, Mode=TwoWay}" StateChanged="{Binding IsMinimized}">

This is my viewmodel.

public ICommand IsMinimized
    {
        get
        {
            if (_IsMinimized == null)
            {
                _IsMinimized = new RelayCommand(param => this.OnMinimized(), null);
            }
            return _IsMinimized;
        }
    }

    private void OnMinimized()
    {
        //do something here
    }

Is there anyother way to do this?

user1890098
  • 473
  • 2
  • 10
  • 24
  • *I bound the `Window.StateChanged` Event to an `ICommand` and it didn't work?*... Really? Why was that? Perhaps because an `ICommand` is *not* an event? – Sheridan Aug 05 '14 at 10:24
  • @RohitVats, how did you manage to close this question as a duplicate *of a question that has no accepted answer*? I didn't think that we could do that. – Sheridan Aug 05 '14 at 10:27
  • Ok this won't work. But is there any other way to do this? I did find a way to bind a RoutedEvent to a command from here [Binding RoutedEvent to Command](http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html) But StateChanged is not a RoutedEvent? Is there any other way to achieve this? – user1890098 Aug 05 '14 at 10:28
  • *Ok this won't work*... why not? It does for everyone else? Alternatively, just handle the `Window.StateChanged` Event in the code behind and in the handler, simply execute your `ICommand`: `var viewModel = (SomeDataType)DataContext; viewModel.YourCommand.Execute(params);` – Sheridan Aug 05 '14 at 10:32
  • @Sheridan - Ok. I will reopen the question but most voted answer definitely work. Not sure how OP using it. – Rohit Vats Aug 05 '14 at 10:34
  • @user1890098 - Can you edit the question with what have you tried and it doesn't work? – Rohit Vats Aug 05 '14 at 10:34
  • @RohitVats, I wasn't requesting that you re-open the question... as I said, I just didn't think that it was possible for users to link to a duplicate question if it had no accepted answer. I guess I must be mistaken. – Sheridan Aug 05 '14 at 11:33
  • @Sheridan - No. its possible to mark duplicate of question if it has any answers. – Rohit Vats Aug 05 '14 at 11:55
  • Thanks for clarifying that... it's good to know. However, this question author clearly has no interest in implementing any of these (or the previously linked) solutions, so perhaps it would be better to re-close this question. – Sheridan Aug 05 '14 at 12:12

2 Answers2

2

Thanks for the all the help. But I ended up binding WindowState to a property and handled the code there.

public WindowState CurrentWindowState
    {
        get { return _currentWindowState; }
        set
        {
            _currentWindowState = value;
            if (_currentWindowState == WindowState.Minimized)  //any other clause here
            {
               //do something here
            }
            NotifyPropertyChanged("CurrentWindowState");
        }
    }
user1890098
  • 473
  • 2
  • 10
  • 24
  • NotifyPropertyChanged is only needed, if you want to change CurrentWindowState from within your code and want to notify the UI about it, so that you are able to minimize/maximize/... your window from your viewmodel – esskar Aug 05 '14 at 12:18
  • to make it better, change your setter to `if (_currentWindowState != value) { _currentWindowState = value; if (_currentWindowState == WindowState.Minimized) { ... } ... }` – esskar Aug 05 '14 at 12:20
1

Yes, you can bind events to your model, but you need help. You need to use functions from the System.Windows.Interactivity Namespace and include a MVVM Light (there might be other MVVM libraries that have that feature but i use MVVM Light).

Include the following namespaces to your window

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"

and bind your events like

<i:Interaction.Triggers>
    <i:EventTrigger EventName="StateChanged">
        <cmd:EventToCommand Command="{Binding StateChangedCommand}" PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>

HTH

esskar
  • 10,638
  • 3
  • 36
  • 57