Add a RowDefinition to Grid

image_pdfimage_print


   
  
<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="ColumnDefinitions Sample">
    <Border BorderBrush="Black" Background="White" BorderThickness="2">
  <DockPanel Margin="10,0,0,0">
    <TextBlock FontSize="20" FontWeight="Bold" DockPanel.Dock="Top">Grid Column and Row Collections</TextBlock>
        <Grid DockPanel.Dock="Top" HorizontalAlignment="Left" Name="grid1" ShowGridLines="true" Width="625" Height="400">
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
          <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
        </Grid>

        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
            <Button Width="125" Click="addRow">Add Row</Button>
        </StackPanel>    

  </DockPanel>
  </Border>  
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        RowDefinition rowDef1;
        ColumnDefinition colDef1;

        private void addRow(object sender, RoutedEventArgs e)
        {
            rowDef1 = new RowDefinition();
            grid1.RowDefinitions.Add(rowDef1);
        }

    }
}

   
    
     


Grid PreviewMouseDown action and MouseDown action

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddHandler" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button>

      <Grid PreviewMouseDown="PreviewMouseDownGrid" MouseDown="MouseDownGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Canvas Width="20" Height="18" VerticalAlignment="Center">

          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16"
                   Fill="Yellow" Stroke="Black" />
        </Canvas>

      </Grid>
    </Button>
  </Grid>
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void PreviewMouseDownGrid(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("PreviewMouseDownGrid"); 
        }

        void MouseDownGrid(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("MouseDownGrid"); 
        }
    }
}

   
    
     


Grid MouseLeftButtonDown action and PreviewMouseLeftButtonDown action

image_pdfimage_print


   
  



<Window x:Class="TunnelingBubbling.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TunnelingBubbling">
  
    <Grid MouseLeftButtonDown="MouseDownGrid"
          PreviewMouseLeftButtonDown="PreviewMouseDownGrid"
      Width="300" Height="300">

    <Button PreviewMouseLeftButtonDown="PreviewMouseDownButton"
      MouseLeftButtonDown="MouseDownButton"
      Click="MyClickEvent"
      Name="btnGo">

      <TextBox MouseLeftButtonDown="MouseLeftButtonDown"
        PreviewMouseLeftButtonDown="PreviewMouseLeftButtonDown"
        Width="200" Height="30" Name="textBox1">
      </TextBox>
    </Button>
  </Grid>  
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace TunnelingBubbling
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void PreviewMouseDownGrid(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseDownGrid");
        }

        private void PreviewMouseDownButton(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseDownButton");
        }

        private void PreviewMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseLeftButtonDown");
        }

        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MyClickEvent");
        }

        private void MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseLeftButtonDown");
        }

        private void MouseDownButton(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseDownButton");
        }

        private void MouseDownGrid(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseDownGrid");
        }

    }
}

   
    
     


Define a GridSplitter and ShowsPreview Changed

image_pdfimage_print


   
  

<Window x:Class="GridSplitter_Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitter Example">
<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Name="Col0" />
    <ColumnDefinition Name="Col1" />
    <ColumnDefinition Name="Col2" />
    <ColumnDefinition Name="Col3" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Name="Row0" />
    <RowDefinition Name="Row1" />
    <RowDefinition Name="Row2" />
  </Grid.RowDefinitions>

  <StackPanel Grid.Row="0" Grid.Column="0" Background="Orange">
    <TextBlock>Row 0 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="0" Background="Blue">
    <TextBlock>Row 1 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="0" Background="Green">
    <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="1" Background="Purple">
    <TextBlock>Row 0 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="1" Background="Red">
    <TextBlock>Row 1 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="1" Background="Salmon">
   <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="2" Background="MediumVioletRed">
    <TextBlock>Row 0 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="2" Background="SteelBlue">
    <TextBlock>Row 1 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="2" Background="Olive">
    <TextBlock>Row 2 Col 2</TextBlock>
  </StackPanel>
   
  <GridSplitter Name="myGridSplitter" Grid.Column="1" Grid.Row="1" Width="5"/>
  <StackPanel Grid.Column="3" Grid.RowSpan="3" Background="Brown">
    <TextBlock FontSize="14" Foreground="Yellow">Property Settings</TextBlock>
      <StackPanel Margin="0,5,0,0">
        <TextBlock>ShowsPreview</TextBlock>
        <RadioButton Name="ShowsPreviewFalse" Checked="ShowsPreviewChanged" IsChecked="true" GroupName="ShowsPreviewProperty">
      false (default)
        </RadioButton>
        <RadioButton Name="ShowsPreviewTrue" Checked="ShowsPreviewChanged" GroupName="ShowsPreviewProperty">
         true
        </RadioButton>
      </StackPanel>
  </StackPanel>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GridSplitter_Example
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }

        private void ShowsPreviewChanged(object sender, RoutedEventArgs e)
        {
          if ((Boolean)ShowsPreviewFalse.IsChecked)
            myGridSplitter.ShowsPreview = false;
          else if ((Boolean)ShowsPreviewTrue.IsChecked)
            myGridSplitter.ShowsPreview = true;
        }
  
  }
}

   
    
     


GridSplitter and ResizeBehaviorChanged

image_pdfimage_print


   
  

<Window x:Class="GridSplitter_Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitter Example">
<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Name="Col0" />
    <ColumnDefinition Name="Col1" />
    <ColumnDefinition Name="Col2" />
    <ColumnDefinition Name="Col3" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Name="Row0" />
    <RowDefinition Name="Row1" />
    <RowDefinition Name="Row2" />
  </Grid.RowDefinitions>

  <StackPanel Grid.Row="0" Grid.Column="0" Background="Orange">
    <TextBlock>Row 0 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="0" Background="Blue">
    <TextBlock>Row 1 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="0" Background="Green">
    <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="1" Background="Purple">
    <TextBlock>Row 0 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="1" Background="Red">
    <TextBlock>Row 1 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="1" Background="Salmon">
   <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="2" Background="MediumVioletRed">
    <TextBlock>Row 0 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="2" Background="SteelBlue">
    <TextBlock>Row 1 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="2" Background="Olive">
    <TextBlock>Row 2 Col 2</TextBlock>
  </StackPanel>
   
  <GridSplitter Name="myGridSplitter" Grid.Column="1" Grid.Row="1" Width="5"/>
  <StackPanel Grid.Column="3" Grid.RowSpan="3" Background="Brown">
    <TextBlock FontSize="14" Foreground="Yellow">Property Settings</TextBlock>
    <StackPanel Margin="0,5,0,0">
      <TextBlock>GridResizeBehavior</TextBlock>
      <RadioButton Name="BehaviorBasedOnAlignment" Checked="ResizeBehaviorChanged" IsChecked="true" GroupName="GridResizeBehaviorProperty">
       BasedOnAlignment (default)
      </RadioButton>
      <RadioButton Name="BehaviorCurrentAndNext" Checked="ResizeBehaviorChanged" GroupName="GridResizeBehaviorProperty">
       CurrentAndNext
      </RadioButton>
      <RadioButton Name="BehaviorPreviousAndCurrent" Checked="ResizeBehaviorChanged" GroupName="GridResizeBehaviorProperty">
       PreviousAndCurrent
      </RadioButton>
      <RadioButton Name="BehaviorPreviousAndNext" Checked="ResizeBehaviorChanged" GroupName="GridResizeBehaviorProperty">
       PreviousAndNext
      </RadioButton>
    </StackPanel>
  </StackPanel>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GridSplitter_Example
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }

        private void ResizeBehaviorChanged(object sender, RoutedEventArgs e)
        {
          if ((Boolean)BehaviorBasedOnAlignment.IsChecked)
            myGridSplitter.ResizeBehavior = GridResizeBehavior.BasedOnAlignment;
          else if ((Boolean)BehaviorCurrentAndNext.IsChecked)
            myGridSplitter.ResizeBehavior = GridResizeBehavior.CurrentAndNext;
          else if ((Boolean)BehaviorPreviousAndCurrent.IsChecked)
            myGridSplitter.ResizeBehavior = GridResizeBehavior.PreviousAndCurrent;
          else if ((Boolean)BehaviorPreviousAndNext.IsChecked)
            myGridSplitter.ResizeBehavior = GridResizeBehavior.PreviousAndNext;
        }
  
  }
}

   
    
     


Define a GridSplitter and Resize Direction Changed

image_pdfimage_print


   
  
<Window x:Class="GridSplitter_Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitter Example">
<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Name="Col0" />
    <ColumnDefinition Name="Col1" />
    <ColumnDefinition Name="Col2" />
    <ColumnDefinition Name="Col3" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Name="Row0" />
    <RowDefinition Name="Row1" />
    <RowDefinition Name="Row2" />
  </Grid.RowDefinitions>

  <StackPanel Grid.Row="0" Grid.Column="0" Background="Orange">
    <TextBlock>Row 0 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="0" Background="Blue">
    <TextBlock>Row 1 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="0" Background="Green">
    <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="1" Background="Purple">
    <TextBlock>Row 0 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="1" Background="Red">
    <TextBlock>Row 1 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="1" Background="Salmon">
   <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="2" Background="MediumVioletRed">
    <TextBlock>Row 0 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="2" Background="SteelBlue">
    <TextBlock>Row 1 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="2" Background="Olive">
    <TextBlock>Row 2 Col 2</TextBlock>
  </StackPanel>
   
  <GridSplitter Name="myGridSplitter" Grid.Column="1" Grid.Row="1" Width="5"/>
  <StackPanel Grid.Column="3" Grid.RowSpan="3" Background="Brown">
    <TextBlock FontSize="14" Foreground="Yellow">Property Settings</TextBlock>
      <StackPanel Margin="0,5,0,0">
        <TextBlock>ResizeDirection</TextBlock>
        <RadioButton Name="ResizeDirectionAuto" Checked="ResizeDirectionChanged" IsChecked="true" GroupName="ResizeDirectionProperty">
        Auto (default)
        </RadioButton>
        <RadioButton Name="ResizeDirectionCols" Checked="ResizeDirectionChanged" GroupName="ResizeDirectionProperty">
        Columns
        </RadioButton>
        <RadioButton Name="ResizeDirectionRows" Checked="ResizeDirectionChanged" GroupName="ResizeDirectionProperty">
        Rows
        </RadioButton>
      </StackPanel>
  </StackPanel>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GridSplitter_Example
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }

        private void ResizeDirectionChanged(object sender, RoutedEventArgs e)
        {
          if ((Boolean)ResizeDirectionAuto.IsChecked)
            myGridSplitter.ResizeDirection = GridResizeDirection.Auto;
          else if ((Boolean)ResizeDirectionCols.IsChecked)
            myGridSplitter.ResizeDirection = GridResizeDirection.Columns;
          else if ((Boolean)ResizeDirectionRows.IsChecked)
            myGridSplitter.ResizeDirection = GridResizeDirection.Rows;
        }
  
  }
}

   
    
     


Define a GridSplitter and Keyboard Increment

image_pdfimage_print


   
  

<Window x:Class="GridSplitter_Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitter Example">
<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Name="Col0" />
    <ColumnDefinition Name="Col1" />
    <ColumnDefinition Name="Col2" />
    <ColumnDefinition Name="Col3" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Name="Row0" />
    <RowDefinition Name="Row1" />
    <RowDefinition Name="Row2" />
  </Grid.RowDefinitions>

  <StackPanel Grid.Row="0" Grid.Column="0" Background="Orange">
    <TextBlock>Row 0 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="0" Background="Blue">
    <TextBlock>Row 1 Col 0</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="0" Background="Green">
    <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="1" Background="Purple">
    <TextBlock>Row 0 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="1" Background="Red">
    <TextBlock>Row 1 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="1" Background="Salmon">
   <TextBlock>Row 2 Col 1</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="0" Grid.Column="2" Background="MediumVioletRed">
    <TextBlock>Row 0 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="1" Grid.Column="2" Background="SteelBlue">
    <TextBlock>Row 1 Col 2</TextBlock>
  </StackPanel>
  <StackPanel Grid.Row="2" Grid.Column="2" Background="Olive">
    <TextBlock>Row 2 Col 2</TextBlock>
  </StackPanel>
   
  <GridSplitter Name="myGridSplitter" Grid.Column="1" Grid.Row="1" Width="5"/>
  <StackPanel Grid.Column="3" Grid.RowSpan="3" Background="Brown">
    <TextBlock FontSize="14" Foreground="Yellow">Property Settings</TextBlock>

      <StackPanel Margin="0,5,0,0">
        <TextBlock>KeyboardIncrement</TextBlock>
        <RadioButton Name="KeyboardIncrementAuto" Checked="KeyboardIncrementChanged" IsChecked="true" GroupName="KeyboardIncrementProperty">
        1 (default)
        </RadioButton>
        <RadioButton Name="KeyboardIncrementCols" Checked="KeyboardIncrementChanged" GroupName="KeyboardIncrementProperty">
        20
        </RadioButton>
        <RadioButton Name="KeyboardIncrementRows" Checked="KeyboardIncrementChanged" GroupName="KeyboardIncrementProperty">
        50
        </RadioButton>
        <RadioButton Name="KeyboardIncrementBoth" Checked="KeyboardIncrementChanged" GroupName="KeyboardIncrementProperty">
        100
        </RadioButton>
      </StackPanel>
  </StackPanel>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GridSplitter_Example
{
  public partial class Window1 : Window
  {
      public Window1()
      {
        InitializeComponent();
      }

        private void KeyboardIncrementChanged(object sender, RoutedEventArgs e)
        {
          if ((Boolean)KeyboardIncrementAuto.IsChecked)
            myGridSplitter.KeyboardIncrement = 1;
          else if ((Boolean)KeyboardIncrementCols.IsChecked)
            myGridSplitter.KeyboardIncrement = 20;
          else if ((Boolean)KeyboardIncrementRows.IsChecked)
            myGridSplitter.KeyboardIncrement = 50;
          else if ((Boolean)KeyboardIncrementBoth.IsChecked)
            myGridSplitter.KeyboardIncrement = 100;
        }
  
  }
}