Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

WPF: Multi-Column Multi-Row Grid

 

This example shows a dynamic grid with multiple columns and rows.

Whenever the mouse cursor moves over a cell, the position of the cell is recorded and displayed

 

 

 

In Visual Studio

 

The code creates a background border for each row and column.

An event on MouseEnter and MouseLeave is given dynamically to each cell.

            int iRow = -1;

            foreach(RowDefinition row in BaseGrid.RowDefinitions)

            {

                iRow++;

                int iCol = -1;

                foreach (ColumnDefinition col in BaseGrid.ColumnDefinitions)

                {

                    iCol++;

                    Border panel = new Border();

                    Grid.SetColumn(panel,iCol);

                    Grid.SetRow(panel,iRow);

 

                    ..

                    panel.MouseEnter += Panel_MouseEnter;

                    panel.MouseLeave += Panel_MouseLeave; ;

 

                    panel.Margin = new Thickness(1);

                    panel.Background = new SolidColorBrush(Color.FromArgb(100, 100, 100, 100));

 

                    BaseGrid.Children.Add(panel);

                }

            }

 

As soon as the cursor moves over a cell in the grid, the MouseEvent is passed through MouseEnter and MouseLeave.

private void Panel_MouseLeave(object sender, MouseEventArgs e)

        {

            Border panel = sender as Border;

            panel.BorderThickness = new Thickness(0);           

        }

 

        private void Panel_MouseEnter(object sender, MouseEventArgs e)

        {

            Border panel = sender as Border;

            panel.BorderThickness = new Thickness(1);

            panel.BorderBrush = new SolidColorBrush(Color.FromArgb(200, 20, 20,  20));

            string sCol = " col=" + Grid.GetColumn(panel);

            string sRow = " row=" + Grid.GetRow(panel);

            lblStatus.Content = "border0_MouseMove " + sCol + sRow ;

        }

 

 

 

C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

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.Navigation;

using System.Windows.Shapes;

 

namespace test_wpf_DynaGrid

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

 

            init_Panels();

        }

 

      public void init_Panels()

        {

            int iRow = -1;

            foreach(RowDefinition row in BaseGrid.RowDefinitions)

            {

                iRow++;

                int iCol = -1;

                foreach (ColumnDefinition col in BaseGrid.ColumnDefinitions)

                {

                    iCol++;

                    Border panel = new Border();

                    Grid.SetColumn(panel,iCol);

                    Grid.SetRow(panel,iRow);

 

                    Label lbl = new Label();

                    lbl.Content="col" + iCol + " row" + iRow;

                    lbl.HorizontalAlignment = HorizontalAlignment.Center;

                    lbl.VerticalAlignment = VerticalAlignment.Center;

                    panel.Child = lbl;

                    panel.MouseEnter += Panel_MouseEnter;

                    panel.MouseLeave += Panel_MouseLeave; ;

                    panel.Margin = new Thickness(1);

                    panel.Background = new SolidColorBrush(Color.FromArgb(100, 100, 100, 100));

                    BaseGrid.Children.Add(panel);

                }

            }

 

        }

 

        private void Panel_MouseLeave(object sender, MouseEventArgs e)

        {

            Border panel = sender as Border;

            panel.BorderThickness = new Thickness(0);           

        }

 

        private void Panel_MouseEnter(object sender, MouseEventArgs e)

        {

            Border panel = sender as Border;

            panel.BorderThickness = new Thickness(1);

            panel.BorderBrush = new SolidColorBrush(Color.FromArgb(200, 20, 20,  20));

            string sCol = " col=" + Grid.GetColumn(panel);

            string sRow = " row=" + Grid.GetRow(panel);

            lblStatus.Content = "border0_MouseMove " + sCol + sRow ;

        }

    }

}

 

 

 

Xaml code

<Window x:Class="test_wpf_DynaGrid.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:test_wpf_DynaGrid"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525" >

    <Grid x:Name="BaseGrid"  >

        <Grid.ColumnDefinitions >

            <ColumnDefinition Width="121*" />

            <ColumnDefinition Width="67*"/>

            <ColumnDefinition Width="64*"/>

            <ColumnDefinition Width="57*" />

            <ColumnDefinition Width="55*"/>

            <ColumnDefinition Width="154*"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="36*" />

            <RowDefinition Height="20*"/>

            <RowDefinition Height="16*"/>

            <RowDefinition Height="49*" />

            <RowDefinition Height="39*"/>

        </Grid.RowDefinitions>

 

 

        <Label x:Name="lblStatus"  Background="Yellow" Height="30" VerticalAlignment="Top" Grid.ColumnSpan="2" Margin="0,0,67,0"  >

 

        </Label>

    </Grid>

</Window>

 

 

Mobile
»
WPF: Grid with variable cells
»
WPF: Multi-Column Multi-Row Grid with Cell Events
»
WPF: Change desktop background dynamically

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de