Link to home
Start Free TrialLog in
Avatar of locdang
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.

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

Avatar of saragani
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

ASKER

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

ASKER

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

ASKER

see here as to why it is not done in XAML: http://stackoverflow.com/questions/457689/wpf-custom-window

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

Maybe this can help:
http://www.codeproject.com/KB/WPF/CustomFrames.aspx
http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf.aspx

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

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
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

ASKER

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