The Mvvm-Extensions
library provides base implementations of classes, required for WPF application which intends to utilize MVVM pattern. Additionaly, the library provides a few solutions to common problems that arise during GUI development.
PM> Install-Package Mvvm-Extensions -Version 1.4.0
This library provides a few mechanisms that allow to greatly simplify the initial development of WPF application that utilizes MVVM pattern.
PropertyChangedImplementation
is a class that provides base implementation of the essential INotifyPropertyChanged
interface.
Classes, that inherit PropertyChangedImplementation
can invoke PropertyChanged
event by calling NotifyPropertyChanged()
method in their property setters. Name of the invoking property is pulled automatically.
class RectangleViewModel : PropertyChangedImplementation
{
int width = 0;
int height = 0;
public int Width
{
get
{
return width;
}
set
{
if (width != value)
{
width = value;
NotifyPropertyChanged();
}
}
}
public int Height
{
get
{
return height;
}
set
{
if (height != value)
{
height = value;
NotifyPropertyChanged();
}
}
}
}
In addition, the library contains two ways to simplify usage of properties, that are dependant upon other properties in their get
accessor.
Lets assume we have three properties in datacontext, all of which are displayed in the view: width
, height
and area
, where width
and height
are standalone properties and area
is a read-only property that returns a product of width
and height
properties.
In a normal scenario changing the values of width
or height
properties will not change the displayed value of area
property, unless PropertyChanged
is explicitly invoked for it.
Properties, marked with DependsOn
attribute automatically invoke PropertyChanged
event when this event is invoked for one of the properties, specified in attribute parameters.
DependsOn
attribute should be used in classes that implement INotifyPropertyChanged and either utilize DependencyWatcher
class or inherit PropertyChangedImplementation
class.
This attribute allows to automatically invoke PropertyChanged
event for properties that lack set
accessor or their value depends on other properties.
DependencyWatcher
is an alternative class that allows to use the DependsOn
attribute when its impossible or not desirable to inherit PropertyChangedImplementation
by the viewmodel. The viewmodel must implement INotifyPropertyChanged and provide DependencyWatcher
with a method for PropertyChanged
event invokation.
The resulting behavior of properties marked by DependsOn
attribute is the same between both methods.
DelegateCommand
provides the most basic implementation of ICommand
interface that can be used immediatelly to invoke viewmodel commands from the views.
private bool runningAutomaticOp = false;
public bool RunningAutomaticOp
{
get
{
return runningAutomaticOp;
}
set
{
if (runningAutomaticOp != value)
{
runningAutomaticOp = value;
NotifyPropertyChanged();
}
}
}
[DependsOn(nameof(RunningAutomaticOp))]
public ICommand RunAutomaticOperationCommand
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () => !RunningAutomaticOp,
CommandAction = (x) => RunAutomaticOp()
};
}
}
private void RunAutomaticOp()
{
// Logic
}
<Button Command="{Binding RunAutomaticOperationCommand}" />
CursorState
class allows application to change the widndows cursor icon while it remains in the application window. The cursor automatically reverts back to the default icon when CursorState
instance is disposed.
The following code is used to create behavior visible on the image above:
Application.Current.Dispatcher.Invoke(async () =>
{
using (new CursorState(Cursors.Wait))
{
await Task.Delay(2000);
}
});
The library contains a number of converters that may be useful for complex UI implementations. The available converters are shown below:
- BooleanToVisibilityConverter
- InvertBooleanConverter
- InvertBooleanToVisibilityConverter
- InvertValueToVisibilityConverter
- MultiBoolAndConverter
- MultiBoolOrConverter
- ValueToVisibilityConverter
Converts bool
to System.Windows.Visibility
true
converts to Visibility.Visible
false
converts to converter parameter (default is Visibility.Collapsed
)
Inverts bool
value
Converts bool
to System.Windows.Visibility
true
converts to converter parameter (default is Visibility.Collapsed
)
false
converts to Visibility.Visible
Converts object
to System.Windows.Visibility
null
converts to Visibility.Visible
Any object converts to converter parameter (default is Visibility.Collapsed
)
Converts bool[]
to single bool
value by performing logican AND operation on each member
Converts bool[]
to single bool
value by performing logican OR operation on each member
Converts object
to System.Windows.Visibility
Any object converts to Visibility.Visible
null
converts to converter parameter (default is Visibility.Collapsed
)