Use the Grid to create a dialog box that uses the WPF layout API : Window « Windows Presentation Foundation « C# / C Sharp






Use the Grid to create a dialog box that uses the WPF layout API

Use the Grid to create a dialog box that uses the WPF layout API
   

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Grid_Run_Dialog.Window1"
    Name="mainWindow" Loaded="onLoaded" Width="425" Height="200"/>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Grid_Run_Dialog
{
  public partial class Window1 : Window
  {
    Grid grid1 = new Grid ();
    ColumnDefinition colDef1 = new ColumnDefinition();
    ColumnDefinition colDef2  = new ColumnDefinition();
    ColumnDefinition colDef3 = new ColumnDefinition();
    ColumnDefinition colDef4 = new ColumnDefinition();
    ColumnDefinition colDef5 = new ColumnDefinition();
    RowDefinition rowDef1= new RowDefinition();
    RowDefinition rowDef2= new RowDefinition();
    RowDefinition rowDef3= new RowDefinition();
    RowDefinition rowDef4= new RowDefinition();
    TextBlock txt1 = new TextBlock();
    TextBlock txt2 = new TextBlock();
    Button button1= new Button();
    Button button2= new Button();
    Button button3= new Button();
    TextBox tb1;
    Image img1 = new Image();

        void onLoaded(object sender, EventArgs e)
    {
            grid1.Background = Brushes.Gainsboro;
            grid1.HorizontalAlignment = HorizontalAlignment.Left;
            grid1.VerticalAlignment = VerticalAlignment.Top;
            grid1.ShowGridLines = true;
            grid1.Width = 425;
            grid1.Height = 165;

            colDef1.Width = new GridLength(1, GridUnitType.Auto);
            colDef2.Width = new GridLength(1, GridUnitType.Star);
            colDef3.Width = new GridLength(1, GridUnitType.Star);
            colDef4.Width = new GridLength(1, GridUnitType.Star);
            colDef5.Width = new GridLength(1, GridUnitType.Star);
            grid1.ColumnDefinitions.Add(colDef1);
            grid1.ColumnDefinitions.Add(colDef2);
            grid1.ColumnDefinitions.Add(colDef3);
            grid1.ColumnDefinitions.Add(colDef4);
            grid1.ColumnDefinitions.Add(colDef5);

            rowDef1.Height = new GridLength(1, GridUnitType.Auto);
            rowDef2.Height = new GridLength(1, GridUnitType.Auto);
            rowDef3.Height = new GridLength(1, GridUnitType.Star);
            rowDef4.Height = new GridLength(1, GridUnitType.Auto);
            grid1.RowDefinitions.Add(rowDef1);
            grid1.RowDefinitions.Add(rowDef2);
            grid1.RowDefinitions.Add(rowDef3);
            grid1.RowDefinitions.Add(rowDef4);

            img1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("fil:///c:/image.png", UriKind.Relative));
            Grid.SetRow(img1, 0);
            Grid.SetColumn(img1, 0);

            txt1.Text = "Text";
            txt1.TextWrapping = TextWrapping.Wrap;
            Grid.SetColumnSpan(txt1, 4);
            Grid.SetRow(txt1, 0);
            Grid.SetColumn(txt1, 1);

            txt2.Text = "Open:";
            Grid.SetRow(txt2, 1);
            Grid.SetColumn(txt2, 0);
            
            Grid.SetRow(tb1, 1);
            Grid.SetColumn(tb1, 1);
            Grid.SetColumnSpan(tb1, 5);
            
            button1.Content = "OK";
            button2.Content = "Cancel";
            button3.Content = "Browse ...";
            Grid.SetRow(button1, 3);
            Grid.SetColumn(button1, 2);
            button1.Margin = new Thickness(10, 0, 10, 15);
            button2.Margin = new Thickness(10, 0, 10, 15);
            button3.Margin = new Thickness(10, 0, 10, 15);
            Grid.SetRow(button2, 3);
            Grid.SetColumn(button2, 3);
            Grid.SetRow(button3, 3);
            Grid.SetColumn(button3, 4);
            
            grid1.Children.Add(img1);
            grid1.Children.Add(txt1);
            grid1.Children.Add(txt2);
            grid1.Children.Add(tb1);
            grid1.Children.Add(button1);
            grid1.Children.Add(button2);
            grid1.Children.Add(button3);
            
            mainWindow.Content = grid1;

        }
    }
}

   
    
    
  








Related examples in the same category

1.Basic DialogBoxBasic DialogBox
2.Window ResizeMode=CanMinimizeWindow ResizeMode=CanMinimize
3.Window BackgroundWindow Background
4.Set Window Height and WidthSet Window Height and Width
5.Control the Size of UI Elements in a FormControl the Size of UI Elements in a Form
6.Define the Tab Order of UI Elements in a FormDefine the Tab Order of UI Elements in a Form
7.About DialogAbout Dialog
8.About Dialog - Font Properties on RootAbout Dialog - Font Properties on Root
9.Window mouse down eventWindow mouse down event
10.Window Preview mouse down eventWindow Preview mouse down event
11.Window mouse up eventWindow mouse up event
12.Window OwnershipWindow Ownership
13.Launch a window with defined XAMLLaunch a window with defined XAML
14.Hosting a Windows Forms Property Grid in WPFHosting a Windows Forms Property Grid in WPF
15.Window.DragMoveWindow.DragMove
16.Window.CommandBindingsWindow.CommandBindings
17.Create a non-rectangular windowCreate a non-rectangular window
18.Order of precedence for sizing-related properties that are implemented by Window.Order of precedence for sizing-related properties that are implemented by Window.
19.Use Window Activated and Deactivated event to control a media fileUse Window Activated and Deactivated event to control a media file
20.Custom Element Binding WindowCustom Element Binding Window
21.ShutdownMode.OnLastWindowCloseShutdownMode.OnLastWindowClose
22.Window Loaded eventWindow Loaded event
23.Transparent WindowTransparent Window
24.Animated Video WindowAnimated Video Window
25.Close a window with Escape key pressedClose a window with Escape key pressed
26.Use WindowState to make full screen window, change resize mode to NoReSize
27.Window Closing and Closed eventWindow Closing and Closed event
28.Creating the main panel and add to Window in CodeCreating the main panel and add to Window in Code
29.Is Window activeIs Window active
30.Load the Data for a Window Asynchronously After It Has RenderedLoad the Data for a Window Asynchronously After It Has Rendered
31.Change window cursorChange window cursor
32.Show window based on button nameShow window based on button name
33.Listen to Window loaded eventListen to Window loaded event
34.Create Window and add window closing event handlerCreate Window and add window closing event handler
35.Activate window, close window, bring window to frontActivate window, close window, bring window to front
36.Do not handle events until Window is fully initialized.Do not handle events until Window is fully initialized.
37.Retrieving the mouse position relative to controls on a WindowRetrieving the mouse position relative to controls on a Window
38.Window On Mouse move eventWindow On Mouse move event
39.Window On Mouse up eventWindow On Mouse up event
40.Display window as dialogDisplay window as dialog
41.Load resource from Window ResourcesLoad resource from Window Resources
42.Windows Transparent BackgroundWindows Transparent Background
43.Save Window Position to RegistrySave Window Position to Registry
44.Center a Window to ScreenCenter a Window to Screen
45.Window Preview Key EventsWindow Preview Key Events
46.Non-Rectangular windowNon-Rectangular window
47.Set a Default ButtonSet a Default Button
48.ShowInTaskbar = false
49.SizeToContent=WidthAndHeight
50.ResizeMode=CanResizeWithGrip
51.Set the DataContext of a Window to a person objectSet the DataContext of a Window to a person object
52.DataContext with user defined object
53.Find owner window