Universal Windows Platform – Drag and Drop

Drag and Drop demonstrates how to use a ListBox to create a simple drag-and-drop example

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.ObjectModel;
using System.Linq;

public class Item
{
    public Guid Id { get; set; }
    public string Value { get; set; }
}

public class Library
{
    public ObservableCollection<Item> Items { get; set; } = new ObservableCollection<Item>();

    public void Add(string value)
    {
        Items.Add(new Item
        {
            Id = Guid.NewGuid(),
            Value = value
        });
    }

    public void Remove(Guid id)
    {
        Item result = Items.FirstOrDefault(item => item.Id == id);
        if (result != null)
        {
            Items.Remove(result);
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. There is an Item Class used to represent items in an ObservableCollection. This is represented by the Property Items which is an ObservableCollection of Item in the Library Class. The Add Method takes a string which is used to add an Item to the ObservableCollection along with a Guid used for an Id, and there’s a Remove Method to get an Item by it’s Id and Remove it from the ObservableCollection.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Margin="20" Name="Value"/>
	<ListView Grid.Row="1" VerticalAlignment="Stretch" AllowDrop="True" CanReorderItems="True" SelectionMode="Single" Name="Display">
		<ListView.ItemTemplate>
			<DataTemplate>
				<TextBlock FontFamily="Segoe UI" FontSize="16" Text="{Binding Value}"/>
			</DataTemplate>
		</ListView.ItemTemplate>
	</ListView>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Add" Label="Add" Click="Add_Click"/>
	<AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a Grid with two row – the first of which is a TextBox to enter a Valye to add and the second row is a ListBox to show added items with the AllowDrop and CanReorderItems properties set enabling the Control to support drag-and-drop. The second block of XAML is is the CommandBar which contains the Add – to add to the ListBox and Remove to remove items from the ListBox

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	Display.ItemsSource = library.Items;
}

private void Add_Click(object sender, RoutedEventArgs e)
{
	library.Add(Value.Text);
}

private void Remove_Click(object sender, RoutedEventArgs e)
{
	library.Remove(((Item)Display.SelectedItem).Id);
}

Below the MainPage() Method an instance of the Library Class is created, then in the OnNavigatedTo Event Handler the ListBox has its ItemsSource set to the ObservableCollection in the Library Class, Add_Click will add items to this with the Text from the TextBox and the Remove_Click will use Remove from the Library Class to remove items.

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

After the Application has started running you can then type in some text then tap Add you can then add multiple items, then tap and hold or select with a mouse on any of them to move them up and down the list

ran-drag-and-drop

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s