We help IT Professionals succeed at work.
Get Started

WPF Custom Form

locdang
locdang asked
on
760 Views
Last Modified: 2012-05-11
Hi I am a attempting to make a custom form base class completely coded(without xaml) that can be used for new items in my projects.

The problem is, when i configure a new window item to extend this custom window type the XAML editor still looks the same as always... I imagine that I somehow have to specify where the design time editable region is... but I have no idea how to do this and google has been no help so far...

Attached is my custom form code, I realise it may have ineffciencies at the moment :) so apologies, but if you could help me understand how to resolve this problem it would be greatly appreciated.

Thanks.

Xavier.
// Usage:
<m:BasicDialog x:Class="CustomFormResearch.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:m="clr-namespace:CustomFormResearch" Title="Hello World">
    
</m:BasicDialog>



//custom form code

using System.Runtime.InteropServices;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Effects;
using System.Windows.Media;
using System.Windows;
using System;

namespace CustomFormResearch
{
    public class BasicDialog:Window
    {
        public string DialogTitleCaption = "BasicDialog";

        public const int HT_CAPTION = 0x2;
        public const int WM_NCLBUTTONDOWN = 0xA1;

        public BasicDialog()
        {
            InitializeBase();
        }

        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                                             int Msg, int wParam, int lParam);

        public void InitializeBase()
        {
            #region Component Declarations
            ///
            ///Component Declarations
            ///
            DropShadowEffect dseStaticGlowText;
            DropShadowEffect dseGlowInEffect;

            Setter str_txtblk_dseSetter;

            Style syl_txtblk_GlowStyle;

            Grid grdMainDialogGrid;
            Border bdrTitleBar;
            TextBlock txtblkTitleBar;
            Grid grdDialogGrid;
            Border bdrFooter;
            StackPanel spDialogOptions;
            #endregion
            #region Effects
            ///
            /// dseStaticGlowEffect
            /// 
            dseStaticGlowText = new DropShadowEffect();
            dseStaticGlowText.BlurRadius = 5;
            dseStaticGlowText.Color = Colors.White;
            dseStaticGlowText.RenderingBias = RenderingBias.Quality;
            dseStaticGlowText.ShadowDepth = 0;
            ///
            /// dseGlowInEffect
            /// 
            dseGlowInEffect = new DropShadowEffect();
            dseGlowInEffect.BlurRadius = 5;
            dseGlowInEffect.Color = Colors.White;
            dseGlowInEffect.RenderingBias = RenderingBias.Quality;
            dseGlowInEffect.ShadowDepth = 0;
            #endregion
            #region setters
            ///
            ///str_txtblk_dseSetter
            ///
            str_txtblk_dseSetter = new Setter(TextBox.EffectProperty, dseGlowInEffect);
            #endregion
            #region Styles
            ///
            ///syl_txtblk_GlowStyle
            ///
            syl_txtblk_GlowStyle = new Style(typeof(TextBlock));
            syl_txtblk_GlowStyle.Setters.Add(str_txtblk_dseSetter);
            #endregion
            #region WindowResources
            ///
            /// Window Resources
            ///
            this.Resources.Add("StaticGlowText", dseStaticGlowText);
            this.Resources.Add("GlowInEffect", syl_txtblk_GlowStyle);
            #endregion
            #region Title Bar Configuration
            ///
            /// txtblkTitleBar
            /// 
            txtblkTitleBar = new TextBlock();
            txtblkTitleBar.Name = "txtblkTitleBar";
            txtblkTitleBar.Text = DialogTitleCaption;
            txtblkTitleBar.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            txtblkTitleBar.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            txtblkTitleBar.FontSize = 14;
            txtblkTitleBar.MouseDown += new MouseButtonEventHandler(txtblkTitleBar_MouseDown);
            txtblkTitleBar.Effect = new DropShadowEffect()
            {
                ShadowDepth = 0,
                Color = Colors.White,
                BlurRadius = 3,
                RenderingBias = RenderingBias.Quality
            };
            ///
            /// bdrTitleBar
            ///
            bdrTitleBar = new Border();
            bdrTitleBar.BorderBrush = new SolidColorBrush(Colors.White);
            bdrTitleBar.BorderThickness = new Thickness(0, 0, 0, 1);
            bdrTitleBar.Height = 44;
            bdrTitleBar.Name = "bdrTitleBar";
            bdrTitleBar.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            bdrTitleBar.Padding = new Thickness(50, 15, 0, 0);
            bdrTitleBar.Child = txtblkTitleBar;
            #endregion
            #region DialogGrid
            ///
            /// grdDialogGrid
            ///
            grdDialogGrid = new Grid();
            grdDialogGrid.Margin = new Thickness(0, 50, 0, 47);
            grdDialogGrid.Height = 414;
            grdDialogGrid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            grdDialogGrid.Name = "grdDialogGrid";
            #endregion
            #region DialogFooter
            ///
            /// spDialogOptions
            /// 
            spDialogOptions = new StackPanel();
            spDialogOptions.Name = "spDialogOptions";
            spDialogOptions.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            spDialogOptions.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            spDialogOptions.Orientation = Orientation.Horizontal;
            spDialogOptions.FlowDirection = System.Windows.FlowDirection.RightToLeft;
            spDialogOptions.Margin = new Thickness(0, 10, 7, 0);
            spDialogOptions.Width = 965;
            ///
            /// bdrFooter
            ///
            bdrFooter = new Border();
            bdrFooter.Name = "bdrFooter";
            bdrFooter.BorderBrush = new SolidColorBrush(Colors.White);
            bdrFooter.BorderThickness = new Thickness(0, 1, 0, 0);
            bdrFooter.Height = 41;
            bdrFooter.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            bdrFooter.Child = spDialogOptions;
            #endregion
            #region Main Dialog Grid
            ///
            /// grdMainDialogGrid
            /// 
            grdMainDialogGrid = new Grid();
            grdMainDialogGrid.Name = "grdMainDialogGrid";
            grdMainDialogGrid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            grdMainDialogGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            grdMainDialogGrid.Children.Add(bdrTitleBar);
            grdMainDialogGrid.Children.Add(grdDialogGrid);
            grdMainDialogGrid.Children.Add(bdrFooter);
            #endregion
            #region BasicDialog
            ///
            /// BasicDialog
            ///
            this.Background = new SolidColorBrush((Color)new ColorConverter().ConvertFrom("#FF141414"));
            this.Foreground = new SolidColorBrush(Colors.White);
            this.Height = 550;
            this.Width = 1000;
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            this.Title = DialogTitleCaption;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.UseLayoutRounding = false;
            this.KeyUp += new KeyEventHandler(Window_KeyUp);
            this.Content = grdMainDialogGrid;
            #endregion
        }

        private void txtblkTitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                ReleaseCapture();
                SendMessage(new WindowInteropHelper(this).Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                this.Close();
        }
    }
}

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE