Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / WPF

Managing Multiple selection in View Model (.NET Metro Style Apps)

Rate me:
Please Sign up or sign in to vote.
4.44/5 (6 votes)
29 Jun 2012CPOL3 min read 73.6K   3K   11  
This article provides Attached Behavior based approach to manage Multiple Selection in Collection Based UI control from the View Model. All the code in this article is strictly applicable to Win 8 metro style apps. Though the Behaviors can be easily adapted to WPF/Silverlight.
<Page
    x:Class="MultiselectSample.MainPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MultiselectSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i ="using:WinRtBehaviors"
    xmlns:custom="using:WinRtExt.Behavior"
    mc:Ignorable="d">

    <Page.Resources>
        <DataTemplate x:Key="textBlockDataTemplate">
            <TextBlock Width="100" Margin="10" Text="{Binding}"></TextBlock>
   
        </DataTemplate>
    </Page.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <GridView SelectionMode="Multiple" ItemsSource="{Binding Items}" BorderBrush="White" BorderThickness="2" ItemTemplate="{StaticResource textBlockDataTemplate}">
            <i:Interaction.Behaviors>
                <custom:MultiSelectBehavior SelectedItems="{Binding SelectedItems, Mode=TwoWay}">
                    
                </custom:MultiSelectBehavior>
            </i:Interaction.Behaviors>
            </GridView>
            <Rectangle Width="20"></Rectangle>
            <ListView SelectionMode="Multiple" ItemsSource="{Binding Items}"  BorderBrush="White" BorderThickness="2"  ItemTemplate="{StaticResource textBlockDataTemplate}">
                <i:Interaction.Behaviors>
                    <custom:MultiSelectBehavior SelectedItems="{Binding SelectedItems, Mode=TwoWay}">

                    </custom:MultiSelectBehavior>
                </i:Interaction.Behaviors>
            </ListView>
        </StackPanel>
    </Grid>
</Page>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software Engineer based out in Noida.

Technology skillset – .NET, WPF, WCF, LINQ, XAML.

Started blogging on http://1wpf.wordpress.com/


Stackoverflow Profile -> http://stackoverflow.com/users/649524/tilak

Comments and Discussions