Click here to Skip to main content
12,448,343 members (53,809 online)
Click here to Skip to main content

Stats

35.5K views
5.1K downloads
40 bookmarked
Posted

WPF Draggable Label

, 13 Apr 2010 CPOL
WPF label control that can be dragged and resized at runtime
DraggableLabelTest
bin
Release
DraggableLabelTest.exe
DraggableLabelTest.pdb
DraggableLabelTest.vshost.exe
DraggableLabelTest.vshost.exe.manifest
RtwControls.DraggableLabel.dll
RtwControls.DraggableLabel.pdb
WPFToolkit.dll
WPFToolkit.pdb
Properties
DraggableLabel
bin
Release
RtwControls.DraggableLabel.dll
RtwControls.DraggableLabel.pdb
Properties
Themes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using RtwControls;

namespace DraggableLabelTest
{
    /// <summary>
    /// Interaction logic for LayoutWindow.xaml
    /// </summary>
    public partial class LayoutWindow : Window
    {
        public LayoutWindow()
        {
            InitializeComponent();
        }

        private void draggableLabel_Drag(object sender, DraggableLabelDragEventArgs e)
        {
            controlDetails.Content = sender.ToString();
        }

        private void draggableLabel_Resize(object sender, DraggableLabelResizeEventArgs e)
        {
            controlDetails.Content = sender.ToString();
        }

        private void defaultButton_Click(object sender, RoutedEventArgs e)
        {
            SetDefaultLauout();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveLayout();
            MessageBox.Show("Successfully Saved.", "Layout", MessageBoxButton.OK, MessageBoxImage.Information);
            this.Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SetDefaultLauout()
        {
            nameLabel.RenderTransform = new TranslateTransform(0, 0);
            addressLabel.RenderTransform = new TranslateTransform(0, 0);
            emailLabel.RenderTransform = new TranslateTransform(0, 0);
            countryLabel.RenderTransform = new TranslateTransform(0, 0);
            ageLabel.RenderTransform = new TranslateTransform(0, 0);
            genderLabel.RenderTransform = new TranslateTransform(0, 0);
            dateLabel.RenderTransform = new TranslateTransform(0, 0);

            nameLabel.Width = double.NaN;
            nameLabel.Height = double.NaN;
            addressLabel.Width = double.NaN;
            addressLabel.Height = double.NaN;
            emailLabel.Width = double.NaN;
            emailLabel.Height = double.NaN;
            countryLabel.Width = double.NaN;
            countryLabel.Height = double.NaN;
            ageLabel.Width = double.NaN;
            ageLabel.Height = double.NaN;
            genderLabel.Width = double.NaN;
            genderLabel.Height = double.NaN;
            dateLabel.Width = double.NaN;
            dateLabel.Height = double.NaN;
        }

        private void SaveLayout()
        {
            var layout = new XElement("Layout",
                    new XElement("NameLabel",
                        new XAttribute("Top", nameLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", nameLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", nameLabel.ActualWidth),
                        new XAttribute("Height", nameLabel.ActualHeight)
                    ),
                    new XElement("AddressLabel",
                        new XAttribute("Top", addressLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", addressLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", addressLabel.ActualWidth),
                        new XAttribute("Height", addressLabel.ActualHeight)
                    ),
                     new XElement("EmailLabel",
                        new XAttribute("Top", emailLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", emailLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", emailLabel.ActualWidth),
                        new XAttribute("Height", emailLabel.ActualHeight)
                    ),
                     new XElement("CountryLabel",
                        new XAttribute("Top", countryLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", countryLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", countryLabel.ActualWidth),
                        new XAttribute("Height", countryLabel.ActualHeight)
                    ),
                     new XElement("AgeLabel",
                        new XAttribute("Top", ageLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", ageLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", ageLabel.ActualWidth),
                        new XAttribute("Height", ageLabel.ActualHeight)
                    ),
                     new XElement("GenderLabel",
                        new XAttribute("Top", genderLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", genderLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", genderLabel.ActualWidth),
                        new XAttribute("Height", genderLabel.ActualHeight)
                    ),
                     new XElement("DateLabel",
                        new XAttribute("Top", dateLabel.TranslatePoint(new Point(0, 0), this).Y),
                        new XAttribute("Left", dateLabel.TranslatePoint(new Point(0, 0), this).X),
                        new XAttribute("Width", dateLabel.ActualWidth),
                        new XAttribute("Height", dateLabel.ActualHeight)
                    )
                );

            layout.Save("layout.xml");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (File.Exists("layout.xml"))
            {
                XElement layout = XElement.Load("layout.xml");

                var elements = layout.Elements().Select(
                    el => new { Name = el.Name, Top = (double)el.Attribute("Top"), Left = (double)el.Attribute("Left"), Width = (double)el.Attribute("Width"), Height = (double)el.Attribute("Height") });

                var element = elements.Where(el => el.Name == "NameLabel").Single();
                var initialLocation = nameLabel.TranslatePoint(new Point(0, 0), this);
                nameLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                nameLabel.Width = element.Width;
                nameLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "AddressLabel").Single();
                initialLocation = addressLabel.TranslatePoint(new Point(0, 0), this);
                addressLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                addressLabel.Width = element.Width;
                addressLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "EmailLabel").Single();
                initialLocation = emailLabel.TranslatePoint(new Point(0, 0), this);
                emailLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                emailLabel.Width = element.Width;
                emailLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "CountryLabel").Single();
                initialLocation = countryLabel.TranslatePoint(new Point(0, 0), this);
                countryLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                countryLabel.Width = element.Width;
                countryLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "AgeLabel").Single();
                initialLocation = ageLabel.TranslatePoint(new Point(0, 0), this);
                ageLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                ageLabel.Width = element.Width;
                ageLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "GenderLabel").Single();
                initialLocation = genderLabel.TranslatePoint(new Point(0, 0), this);
                genderLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                genderLabel.Width = element.Width;
                genderLabel.Height = element.Height;

                element = elements.Where(el => el.Name == "DateLabel").Single();
                initialLocation = dateLabel.TranslatePoint(new Point(0, 0), this);
                dateLabel.RenderTransform = new TranslateTransform(element.Left - initialLocation.X, element.Top - initialLocation.Y);
                dateLabel.Width = element.Width;
                dateLabel.Height = element.Height;
            }
        }
    }
}

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)

Share

About the Author

Rajitha Wimalasooriya
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 13 Apr 2010
Article Copyright 2010 by Rajitha Wimalasooriya
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid