Link to home
Start Free TrialLog in
Avatar of locdang

asked on

WPF Custom Form

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.


// Usage:
<m:BasicDialog x:Class="CustomFormResearch.MainWindow"
             xmlns:m="clr-namespace:CustomFormResearch" Title="Hello World">

//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()

        public static extern bool ReleaseCapture();

        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;
            #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;
            #region setters
            str_txtblk_dseSetter = new Setter(TextBox.EffectProperty, dseGlowInEffect);
            #region Styles
            syl_txtblk_GlowStyle = new Style(typeof(TextBlock));
            #region WindowResources
            /// Window Resources
            this.Resources.Add("StaticGlowText", dseStaticGlowText);
            this.Resources.Add("GlowInEffect", syl_txtblk_GlowStyle);
            #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;
            #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";
            #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;
            #region Main Dialog Grid
            /// grdMainDialogGrid
            grdMainDialogGrid = new Grid();
            grdMainDialogGrid.Name = "grdMainDialogGrid";
            grdMainDialogGrid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            grdMainDialogGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            #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;

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

        private void Window_KeyUp(object sender, KeyEventArgs e)
            if (e.Key == Key.Escape)

Open in new window

Avatar of saragani

But why???

I'm working with WPF for a year now and never had to build UI in code.

Even in Winform you don't need to do it, since you do it all from the designer... You could have seen the c#-code that is being generated by the designer, but you didn't have to change it.
Avatar of locdang


I am happy to do it in xaml, the the same problem remains. Windows that extend from my base still so not inherret the design of the base class.
Avatar of locdang


And I also do not know how to designate a 'grid' in my base window to function the same as the initial grid you receive in a blank window that allows you to place controls onto it.
Avatar of locdang


see here as to why it is not done in XAML:

xaml inheritance is not possible.
I know that you can't inherit xaml...

Maybe this can help:

The way you are trying to do it now is not the right way....

You need to define your own window and then define it's template (written in XAML).
Avatar of locdang


Isn't 'defining my own window' exactly what I have done? I mean I obviously can't do that in XAML?

Avatar of saragani

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Btw, I don't know if you've noticed what happens if you add content to your main window (for example a button). The new content takes over the content you have defined in code...
Your way will never work... You have created a custom window that cannot accept any content.
Avatar of locdang


I am not understanding. Thanks anyway. I will award the points anyway.