C#, WPF, vb.Net, vba, SQL Server, Access Programmierer, Excel, ASP Core MVC Webforms Telefon: 07022/9319004 freib. Software Entwickler   Kontakt
#

WPF: Multi-Column Multi-Row Grid

 

Dieses Beispiel zeigt ein dynamisches Grid mit mehreren Spalten und Zeilen.

Dabei wird jedesmal wenn der Mauszeiger über eine Zelle fährt, die Position der Zelle erfasst und angezeigt

 

 

 

In Visual Studio

 

Der Code erstellt für jede Zeile und Spalte ein Background Border.

Dabei wird jeder Zelle ein Event on MouseEnter und MouseLeave dynamisch mitgegeben.

            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);

                }

            }

 

Sobald dann der Cursor über eine Zelle im Grid fährt, wird der Mausevent MouseEnter und MouseLeave durchlaufen.

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 mit veränderbaren Zellen
»
WPF: Multi-Column Multi-Row Grid mit Zell-Event
»
WPF: Desktop Hintergrund dynamisch ändern

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Raimund.Popp@Microsoft-Programmierer.de