Drag and Drop (and re-order) to ListBox binded to XML

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Would you please help me with this scenario:

On stage, 2 listboxes and a textbox.
1.The first listbox lists all the items available, the list is grouped by 'Type' and its source is a binded XML file (read only).
2.The second listbox list all the items the user has dropped into from ListBox#1 and it should read from an XML at start-up and save to it on quitting.
3.The textbox list the details of the currently selected item in ListBox#2

snap.gif

My knowledge (and search on internet ;) ) allowed me to achieve binding, grouping with an expander and perhaps the first step to drag item (to be verified)..but I'm stucked at dropping AND re-ordering items on ListBox#2..then saving to the XML!!

Would you please help (even in c#..I'll try to do the translation) ? I join a graphic and the project file (with XML files inside) to help you help me! Thanks!!

The XAML

VB.NET:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
    Title="MainWindow" Height="400" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="Resource3">
                <Label Content="{Binding XPath=Name}"/>
            </DataTemplate>
            <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
                <EventSetter Event="ListBoxItem.PreviewMouseLeftButtonDown" Handler="s_PreviewMouseLeftButtonDown" />
            </Style>
            <Style x:Key="ListBoxItemStyle2" TargetType="{x:Type ListBoxItem}">
                <Setter Property="AllowDrop" Value="true"/>
                <EventSetter Event="ListBoxItem.PreviewMouseLeftButtonDown" Handler="s_PreviewMouseLeftButtonDown" />
                <EventSetter Event="ListBoxItem.Drop" Handler="listbox_Drop"/>
            </Style>
        </Grid.Resources>
        <Grid Name="Grid01">
            <Grid.Resources>
                <CollectionViewSource x:Key="cvsSystems" Source="{Binding XPath=System}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="@Type"/>
                        <scm:SortDescription PropertyName="Name"/>
                    </CollectionViewSource.SortDescriptions>
                    <CollectionViewSource.GroupDescriptions>
                        <dat:PropertyGroupDescription  PropertyName="@Type"/>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </Grid.Resources>
            <ListBox ItemsSource="{Binding Source={StaticResource cvsSystems}}" ItemTemplate="{StaticResource Resource3}" SelectedValuePath="Name" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" x:Name="ListBox1" Width="160" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" Margin="0,0,0,0">
                <ListBox.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Expander Header="{Binding Name}" IsExpanded="True">
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListBox.GroupStyle>
            </ListBox>
        </Grid>
        <Grid Name="Grid02">
            <Grid.Resources>
                <CollectionViewSource x:Key="cvsPreferences" Source="{Binding XPath=System}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="@Type"/>
                        <scm:SortDescription PropertyName="Name"/>
                    </CollectionViewSource.SortDescriptions>
                    <CollectionViewSource.GroupDescriptions>
                        <dat:PropertyGroupDescription  PropertyName="@Type"/>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </Grid.Resources>
            <ListBox ItemsSource="{Binding Source={StaticResource cvsPreferences}}" ItemTemplate="{StaticResource Resource3}" SelectedValuePath="Name" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" x:Name="ListBox2" Width="160" ItemContainerStyle="{DynamicResource ListBoxItemStyle2}" Margin="170,0,0,0"/>
            <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="385,320,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        </Grid>
        <TextBox DataContext="{Binding SelectedItem, ElementName=ListBox2}" Text="{Binding UpdateSourceTrigger=PropertyChanged, XPath=Detail}" Margin="340,0,0,0" x:Name="TextBox1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160"/>
    </Grid>
</Window>

The VB code

VB.NET:
Imports System.IO
Imports System.Xml
Class MainWindow
    Dim sysdata As XmlDocument = New XmlDocument()
    Dim prefdata As XmlDocument = New XmlDocument()
    Dim systemdata As XmlDataProvider = New XmlDataProvider()
    Dim preferencedata As XmlDataProvider = New XmlDataProvider()
    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Initialisation XML
        sysdata.Load(System.AppDomain.CurrentDomain.BaseDirectory & "Systems.xml")
        prefdata.Load(System.AppDomain.CurrentDomain.BaseDirectory & "Preferences.xml")
        systemdata.Document = sysdata
        preferencedata.Document = prefdata
        systemdata.XPath = "Systems"
        preferencedata.XPath = "Systems"
        Grid01.DataContext = systemdata
        Grid02.DataContext = preferencedata
    End Sub

    Private Sub s_PreviewMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        If TypeOf sender Is ListBoxItem Then
            Dim draggedItem As ListBoxItem = TryCast(sender, ListBoxItem)
            DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Copy)
            draggedItem.IsSelected = True
        End If
    End Sub

    Private Sub listbox_Drop(ByVal sender As Object, ByVal e As DragEventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        preferencedata.Document.Save(System.AppDomain.CurrentDomain.BaseDirectory & "Preferences.xml")
    End Sub
End Class
 

Attachments

  • DragDropLB.zip
    17.8 KB · Views: 33
Last edited by a moderator:
Top Bottom