Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / WPF

WPF Gadget Container Control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (13 votes)
6 Mar 2009CPOL3 min read 74.2K   4.7K   51  
A Vista-styled gadget container control.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GadgetLibrary;
using GadgetPrototype.Picture;
using GadgetPrototype.Weather;

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

        private void OnGadgetClose(object sender, RoutedEventArgs e)
        {
            var gadgetContainer = sender as GadgetContainer;

            if (gadgetContainer != null)
            {
                _SnapCanvas.Children.Remove(gadgetContainer);
            }
        }

        private void _SolidColorMenu_Click(object sender, RoutedEventArgs e)
        {
            Background = new SolidColorBrush((Color) ColorConverter.ConvertFromString("#4d6a73"));
        }

        private void _AbstractBackgroundMenu_Click(object sender, RoutedEventArgs e)
        {
            var image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri("pack://application:,,,/GadgetPrototype;component/Resources/Images/abstract.png");
            image.EndInit();
            Background = new ImageBrush(image);
        }

        private void _NatureBackgroundMenu_Click(object sender, RoutedEventArgs e)
        {
            var image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri("pack://application:,,,/GadgetPrototype;component/Resources/Images/nature.png");
            image.EndInit();
            Background = new ImageBrush(image);
        }

        private void _AddWeatherGadget_Click(object sender, RoutedEventArgs e)
        {
            var gadgetContainer = new GadgetContainer();
            Canvas.SetTop(gadgetContainer, 100);
            Canvas.SetLeft(gadgetContainer, 100);
            gadgetContainer.OptionButtonType = OptionButtonTypes.Settings;
            gadgetContainer.Close += OnGadgetClose;

            var weatherGadget = new WeatherGadget();
            gadgetContainer.Gadget = weatherGadget;

            _SnapCanvas.Children.Add(gadgetContainer);
        }

        private void _AddPictureGadget_Click(object sender, RoutedEventArgs e)
        {
            var gadgetContainer = new GadgetContainer();
            Canvas.SetTop(gadgetContainer, 100);
            Canvas.SetLeft(gadgetContainer, 100);
            gadgetContainer.OptionButtonType = OptionButtonTypes.Resize;
            gadgetContainer.Close += OnGadgetClose;

            var pictureGadget = new PictureGadget();
            gadgetContainer.Gadget = pictureGadget;

            _SnapCanvas.Children.Add(gadgetContainer);
        }
    }
}

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)
United Kingdom United Kingdom
I'm a software developer living in Northampton, UK, and enjoy working with the latest .Net technologies.

Comments and Discussions