Showing posts with label Blend SDK. Show all posts
Showing posts with label Blend SDK. Show all posts

Tuesday, October 14, 2014

WPF and MVVM - Events

Last time we have seen how to use RelayCommand in ViewModel instead of a click event in View. But what's about all the other events? There is an easy solution where RelayCommand can be used. Therefore we need to add a Reference to the Extension Assembly System.Windows.Interactivity from Blend SDK (for VS2013).



In the XAML part of the View we need to add the namespace that leads to System.Windows.Interactivity from Blend SDK (for VS2013).
<UserControl x:Class="MvvmPassword.LoginView"
             x:Name="This"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
Now we can use i:EventTrigger with the property EventName within Interaction.Triggers to define which event should linked to Command. The Command is defined with the property Command within i:InvokeCommandAction.
        <ComboBox x:Name="UserNames"
                  Grid.Row="1"
                  Grid.Column="1"
                  Margin="4"
                  ItemsSource="{Binding UserNames}"
                  DisplayMemberPath="UserName">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
Next we can just use the RelayCommand in ViewModel, as seen in the last post.
SelectionCommand = new RelayCommand(SelectionChanged);
public ICommand SelectionCommand
{
    get;
    private set;
}
private void SelectionChanged(object parameter)
{
    // ...
}
With the ViewModelBase class, the RelayCommand and the EventTrigger our small MVVM Framework can be taken as a good starting point.

Further Posts

  1. WPF and MVVM - Fundamentals
  2. WPF and MVVM - Commands
  3. WPF and MVVM - Events

Wednesday, April 30, 2014

Sorting Evolution (8) - Sorting a WPF ListView/GridView by clicking on the header - Behaviors (Expression Blend)

Eighth generation

Last time (Seventh Generation) we used Attached Properties to enable sorting within XAML. An alternative, but similar approach is Behaviors (Blend for VS2013, Working with Behaviors) from Expression Blend.

To create a custom Behavior (VS2013) the Behavior class is extended. Therefore we need to add a Reference to the Extension Assembly System.Windows.Interactivity from Blend SDK (for VS2013). To extend the Behavior<T> "using System.Windows.Interactivity" is added.

SortingAttached (Seventh Generation) is replaced by SortingBehavior. This leads to a reduction of code:

public class SortingBehavior : Behavior<ListView>
{
    private Sorting _sorting;
 
    public SortingBehavior()
    {
        _sorting = new Sorting();
    }
 
    protected override void OnAttached()
    {
        AssociatedObject.AddHandler(GridViewColumnHeader.ClickEvent,
                        new RoutedEventHandler(OnColumnHeaderClicked));
    }
 
    protected override void OnDetaching()
    {
        AssociatedObject.RemoveHandler(GridViewColumnHeader.ClickEvent,
                        new RoutedEventHandler(OnColumnHeaderClicked));
    }
 
    private void OnColumnHeaderClicked(object sender, RoutedEventArgs e)
    {
        ListView listView = sender as ListView;
        if (listView == null)
        {
            return;
        }
 
        _sorting.Sort(e.OriginalSource, listView.Items);
    }
}

There is no need to introduce a DependencyProperty. Instead we have the overridden methods OnAttached and OnDetaching. These methods add or remove the event handler (OnColumnHeaderClicked) that is executed if the column header is clicked. The method OnColumnHeaderClicked is nearly the same as in the Seventh Generation, but we do not need explicitly create and remember a Sorting object, because each ListView gets its own SortingBehavior object, where in the last sample (Seventh Generation) all ListViews have used the identical SortingAttached object.

In the XAML part of the View we need to add the namespace that leads to System.Windows.Interactivity from Blend SDK (for VS2013).

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

Furthermore we remove the Attached Property and set the Behavior.

<ListView x:Name="EighthResultData"
          ItemsSource="{Binding EighthResultData}">
    <i:Interaction.Behaviors>
        <sort:SortingBehavior />
    </i:Interaction.Behaviors>
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding ResultNumber}">
                <GridViewColumnHeader Content="Number"
                                      Padding="2,0,20,0"
                                      HorizontalContentAlignment="Left" />
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding ResultOutput}">
                <GridViewColumnHeader Content="Output"
                                      Padding="2,0,20,0"
                                      HorizontalContentAlignment="Left" />
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

The source code can be downloaded from https://code.msdn.microsoft.com/Sorting-a-WPF-ListView-by-7e9c5e4a

Further Posts

  1. Sorting a WPF ListView/GridView by clicking on the header
  2. Sort Direction Indicators 
  3. Sort in ViewModel 
  4. Sort Direction Indicators with Adorners
  5. Reusability
  6. More Reusability
  7. Attached Property
  8. Behaviors (Expression Blend)