ARTICLE

Event handling with UniformGrid in WPF

Posted by Manish Tewatia Articles | WPF using VB.NET October 20, 2010
In this article you will learn about the uniform grid and Event handling with UniformGrid in WPF.
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor