WPF Event handling with UniformGrid in VB.NET

In this article you will learn about the uniform grid and Event handling with UniformGrid in WPF.
  • 2487

UniformGrid: UniformGrid is a grid that breaks all the rules you've learned about so far: the UniformGrid. Unlike the Grid, the UniformGrid doesn't require (or even support) predefined columns and rows. Instead, you simply set the Rows and Columns properties to set its size. Each cell is always the same size because the available space is divided equally.

In below given example you will see event handling with UniformGrid in WPF:

Example

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <UniformGrid Columns="2" Rows="2" ButtonBase.Click="UniformGrid_Click"
                 MouseDown="UniformGrid_MouseDown">
        <Button Content="Click Here" MaxHeight="25" MaxWidth="70" Name="Button"/>
        <TextBlock Background="BlueViolet" Padding="25,7" Text="Click Here"
               HorizontalAlignment="Center" VerticalAlignment="Center"
               Name="TextBlock"/>
        <Canvas MouseDown="Canvas_MouseDown">
            <Rectangle Canvas.Top="19" Canvas.Left="61" Fill="Pink"
                Height="25" Width="100" Name="Rectangle"/>
            <TextBlock Canvas.Top="24" Canvas.Left="86" Text="Click Here"
                   IsHitTestVisible="False"/>
        </Canvas>
    </UniformGrid>
</
Window>

//File:Window.xaml.vb

Imports System.Windows
Imports System.Windows.Input
Class MainWindow 
    Inherits Window
    Public Sub New()
        InitializeComponent()
    End Sub
    ' Handles the MouseDown event on the Canvas.
    Private Sub Canvas_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Dim a As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement
        MessageBox.Show("Your Mouse Down on " & a.Name, "Canvas")
    End Sub 
    ' Handles the Click event on the UniformGrid.
    Private Sub UniformGrid_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim a As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement
        MessageBox.Show("You Click on " & a.Name, "Uniform Grid")
    End Sub 
    ' Handles the MouseDown event on the UniformGrid.
    Private Sub UniformGrid_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Dim a As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement
        MessageBox.Show("Your Mouse Down on " & a.Name, "Uniform Grid")
    End Sub
End
Class

Output Window

grid.gif

Conclusion

Hope this article will help you to understand the working of Event handling with UniformGrid in WPF.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.