Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / Visual Basic 10

Generic Multi-Select MVVM ListBox Drag and Drop Helper with Custom Feedback for Silverlight 4.0

Rate me:
Please Sign up or sign in to vote.
4.91/5 (3 votes)
11 Apr 2011CPOL9 min read 53.2K   981   13  
This article focuses on developing an MVVM compatible ListBox-to-ListBox drag/drop helper for Silverlight.
<UserControl 
    x:Class="SL_MVVM_DragDropHelper_Sample.Views.Products"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"  
    DataContext="{Binding Path=Main, Source={StaticResource Locator}}"
    mc:Ignorable="d"
    >

    <UserControl.Resources>
        <DataTemplate x:Key="ProductModelTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding product_id}"/>
                <TextBlock Grid.Column="1" Text="{Binding title}" TextTrimming="WordEllipsis"/>
                <TextBlock Grid.Column="2" Text="{Binding tags.Count}" TextAlignment="Right"/>
                <TextBlock Grid.Column="3" Text="{Binding group.group_id}" TextAlignment="Right"/>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
            <TextBlock Text="Products" TextAlignment="Center" Margin="4,0" FontWeight="Bold"/>
            <TextBlock Text="{Binding Products.Count, StringFormat='(\{0\})'}"/>
        </StackPanel>
        <Border Grid.Row="1" VerticalAlignment="Center" Background="#3D0000FF" >
            <Grid Margin="6,0,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="ID"/>
                <TextBlock Grid.Column="1" Text="Title"/>
                <TextBlock Grid.Column="2" Text="Tags" TextAlignment="Right"/>
                <TextBlock Grid.Column="3" Text="Group" TextAlignment="Right"/>
            </Grid>
        </Border>
        <ListBox Name="lbProducts" Grid.Row="2" SelectionMode="Extended"
                 ItemsSource="{Binding Products}"
                 ItemTemplate="{StaticResource ProductModelTemplate}">
            <i:Interaction.Triggers>
                <!-- Trigger for Start Drag (LeftMouseDown does not fire). -->
                <i:EventTrigger EventName="MouseMove">
                    <cmd:EventToCommand Command="{Binding ProductsDragDropHelper.ChildMoveCommand, Mode=OneWay}"
                                        PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>
    </Grid>

</UserControl>

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
Technical Lead
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions