Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

(Extended) Modal Window in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.27/5 (6 votes)
2 Apr 2009CPOL3 min read 38.1K   311   19  
An extension of the code submitted in the article: "Modal Window in Silverlight". This extension wraps the hosted control in a window frame that provides various modal features.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Theming;
using ProModel.ApplicationFramework.Silverlight;
using System.Windows.Resources;
using System.Windows.Media.Imaging;

namespace ProModelWindows.Silverlight.AST.Demo
{
	public partial class MainDemoPage : UserControl
    {
        #region Fields and Properties

        BitmapImage _titleImage;

        #endregion

        #region Construction

        public MainDemoPage()
        {
	        // Required to initialize variables
	        InitializeComponent();
        }

        #endregion

        #region Theme

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            StreamResourceInfo sr = Application.GetResourceStream(new Uri("ProModelWindows.Silverlight.AST.Demo;component/Resources/Exclaimation.png", UriKind.Relative));
            _titleImage = new BitmapImage();
            _titleImage.SetSource(sr.Stream);

            string themeToLoad = @"Themes/Microsoft.Windows.Controls.Theming.ExpressionDark.xaml";
            SetTheme(LayoutRoot as FrameworkElement, themeToLoad);
        }

        public static void SetTheme(FrameworkElement control, string theme)
        {
            Uri uri = new Uri(@"ProModelWindows.Silverlight.AST.Demo;component/" + theme, UriKind.Relative);
            ImplicitStyleManager.SetResourceDictionaryUri(control, uri);
            ImplicitStyleManager.SetApplyMode(control, ImplicitStylesApplyMode.Auto);
            ImplicitStyleManager.Apply(control);
        }

        #endregion

        #region Modal Handling

        private void _btnShowWindow_Click(object sender, RoutedEventArgs e)
        {
            if (_tcTabs.SelectedItem == _tpWndResize)
            {
                ShowWindowedModal(OnDialogClosed, LayoutRoot);
            }
            else if (_tcTabs.SelectedItem == _tpWndNoResize)
            {
                ShowWindowedModalNoResize(OnDialogClosed, LayoutRoot);
            }
            else
            {
                ShowModal(OnDialogClosed, LayoutRoot);
            }
        }

        public void ShowModal(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            ModalControl wmc = new ModalControl(new DialogClose(OnDialogClosed));
            TestUserControl tc = new TestUserControl();
            tc._tblMessage.Text = "A non-windowed modal, renders the UserControl as is (ESC to exit)";
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting, new Size(tc.MinWidth, tc.MinHeight));
        }

        public void ShowWindowedModal(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            WindowedModalControl wmc = new WindowedModalControl(callback, MsgBoxButtons.OKCancelApply);
            TestUserControl tc = new TestUserControl();
            wmc.TitleBarBrush = Resources["ModalHeaderBrush1"] as Brush;
            wmc.FormBackgroundBrush = Resources["ModalBrush1"] as Brush;
            wmc.TitleImage = _titleImage;
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting);
            wmc.AllowResizing = true;
            wmc.AllowMaximize = true;
            wmc.TitleBarText = "It's a Working Title...and it truncates if need be";
        }

        public void ShowWindowedModalNoResize(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            WindowedModalControl wmc = new WindowedModalControl(callback, MsgBoxButtons.OKCancel);
            TestUserControl tc = new TestUserControl();
            tc._tblMessage.Text = "A windowed modal that cannot be resized";
            wmc.TitleBarBrush = Resources["ModalHeaderBrush2"] as Brush;
            wmc.FormBackgroundBrush = Resources["ModalBrush2"] as Brush;
            wmc.TitleImage = _titleImage;
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting, new Size(tc.MinWidth, tc.MinHeight));
            wmc.AllowResizing = false;
            wmc.AllowMaximize = false;
            wmc.TitleBarText = "This can't be resized";
        }

        bool OnDialogClosed(object sender, DialogExit e)
        {
            _tbResult.Text = "Dialog Result: " + e.ToString();
            return true;
        }

        #endregion
    }
}

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

Comments and Discussions