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

WPF: Grid mit veränderbaren Zellen

 

Dieses Beispiel enthält eine komplette WPF Anwendung in C#, welches die beinhalteten Zellen in der Größe dynamisch anpassen läßt.

Beim Erhalten des Focus einer Zelle wird ein veränderbarer Rahmen angezeigt, welcher die Reihen und Spalten eines freien WPF Grids ermöglicht.

Dabei werden die RowDefinition und ColumnDefinitions des Grids automatisch angepasst.

 

 

 

Xaml Code des MainWindow

<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="GridMain" MouseMove="BaseGrid_MouseMove" MouseUp="BaseGrid_MouseUp" ShowGridLines="False"   >

        <Grid.ColumnDefinitions >

            <ColumnDefinition Width="154*" />

            <ColumnDefinition Width="94*"/>

            <ColumnDefinition Width="95*"/>

            <ColumnDefinition Width="92*"/>

            <ColumnDefinition Width="*"/>

 

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="36*" />

            <RowDefinition Height="20*"/>

            <RowDefinition Height="16*"/>

 

        </Grid.RowDefinitions>

 

 

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

 

        </Label>

    </Grid>

</Window>

 

 

C# Code des Maingrids mit dem veränderbaren MainGrid

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;

 

using test_wpf_DynaGrid._UserControls;

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 GridMain.RowDefinitions)

            {

                iRow++;

                int iCol = -1;

                foreach (ColumnDefinition col in GridMain.ColumnDefinitions)

                {

                    iCol++;

 

                    UcPanel ucPanel = new UcPanel();

                    ucPanel.Caption= "col" + iCol + " row" + iRow;

                    Border panel = new Border();

                    Grid.SetColumn(ucPanel ,iCol);

                    Grid.SetRow(ucPanel, iRow);

 

                    GridMain.Children.Add(ucPanel);

                }

            }

 

        }

 

 

        #region mouse dragging

        //-------------< MouseMove() >-------------

        public Boolean isAdjusting = false;

        public int adjust_ColNr = -1;

        public int adjust_RowNr = -1;

        public int adjust_EdgeType = 0;

        public double adjust_Offset_X = 0;

        public double adjust_Offset_Y = 0;

 

        enum EdgeTypes

        {

            TopLeft = 1,

            TopRight = 2,

            BottomLeft = 3,

            BottomRight = 4

        }

        enum Adjust_To_Col

        {

            Left = 1,

            Right = 2,           

        }

        enum Adjust_To_Row

        {

            Top = 1,

            Bottom = 2,

        }

        public void BaseGrid_MouseUp(object sender, MouseButtonEventArgs e)

        {

            //-------------< MouseUp() >-------------

            if (isAdjusting)

            {

                //--< reset open adjusters >--

                try

                {

                    foreach (UIElement child_element in GridMain.Children)

                    {

                        if (Grid.GetRow(child_element) == adjust_RowNr)

                        {

                            if (child_element.GetType() == typeof(UcPanel))

                            {

                                UcPanel panel = child_element as UcPanel;

                                panel.hide_Adjuster();

                            }

                        }

                    }

                }

                catch (Exception)

                {

                    //throw;

                }

                //--</ reset open adjusters >--

 

 

                //< reset >

                isAdjusting = false;

                adjust_ColNr = -1;

                adjust_RowNr  = -1;

                adjust_EdgeType = -1;

                adjust_Offset_X = 0;

                adjust_Offset_Y = 0;

                //</ reset >

            }

            //-------------</ MouseUp() >-------------

        }

        private void BaseGrid_MouseMove(object sender, MouseEventArgs e)

        {

            //------------< MouseMove() >------------

            adjust_Cols_And_Rows(sender, e);

            //-------------</ MouseMove() >------------

        }

 

        private void adjust_Cols_And_Rows(object sender, MouseEventArgs e)

        {

            //-------------< adjust_Cols_And_Rows() >-------------

            if (isAdjusting)

            {

                //< check >

                if (adjust_EdgeType < 1) return;

                //</ check >

 

                if (e.LeftButton != MouseButtonState.Pressed)

                {

                    isAdjusting  = false;

                }

                else

                {

                    //------< MouseButtonState.Pressed >------

                    //< check.Logic >

                    if (adjust_ColNr < 0) return;

                    //</ check.Logic >

 

                    //< mouse position >

                    Point mouse_point = e.GetPosition(this);

                    double mouse_x = mouse_point.X;

                    double mouse_y = mouse_point.Y;

                    //</ mouse position >

 

                    //--< Get Direction >--

                    int adjust_Dir_Cols = 0;

                    if ((adjust_EdgeType== (int) EdgeTypes.TopLeft)|| (adjust_EdgeType == (int)EdgeTypes.BottomLeft))

                    {

                        //< left >

                        adjust_Dir_Cols = (int) Adjust_To_Col.Left;

                        //</ left >

                    }

                    else

                    {

                        //< right >

                        adjust_Dir_Cols = (int)Adjust_To_Col.Right;

                        //</ right >

                    }

 

                    int adjust_Dir_Rows = 0;

                    if ((adjust_EdgeType == (int)EdgeTypes.TopLeft) || (adjust_EdgeType == (int)EdgeTypes.TopRight))

                    {

                        //< up >

                        adjust_Dir_Rows = (int)Adjust_To_Row.Top ;

                        //</ up >

                    }

                    else

                    {

                        //< down >

                        adjust_Dir_Rows = (int)Adjust_To_Row.Bottom ;

                        //</ down >

                    }

                    //--</ Get Direction >--

 

 

                    //-------------< adjust Column >--------------

                    //--< get col_x >--

                    double col_x = 0;

                    for (int i = 0; i < adjust_ColNr; i++)

                    {

                        col_x += GridMain.ColumnDefinitions[i].ActualWidth;

                    }

                    //--</ get col_x >--

 

 

                    if(adjust_Dir_Cols == (int)Adjust_To_Col.Left)

                    {

                        //----< Adjust to Left >----

                        //< check >

                        if (adjust_ColNr < 1) return;

                        //</ check >

 

                        double diff_X = col_x - mouse_x + adjust_Offset_X;

 

                        GridMain.ColumnDefinitions[adjust_ColNr].Width = new GridLength( GridMain.ColumnDefinitions[adjust_ColNr].ActualWidth,GridUnitType.Star);

 

                        //--< set Left Col >--

                        ColumnDefinition colDef = GridMain.ColumnDefinitions[adjust_ColNr-1];

                        double new_Width = colDef.ActualWidth - diff_X;

 

                        //< set >

                        if (new_Width < 0) return;

                        colDef.Width = new GridLength(new_Width);

                        //</ set >

                        //--< set Left Col >--

                        //----</ Adjust to Left >----

                    }

                    else

                    {

                        //----< Adjust to Right >----

                        //< check >

                        if (adjust_ColNr >= GridMain.ColumnDefinitions.Count ) return;

                        //</ check >

 

                        col_x += GridMain.ColumnDefinitions[adjust_ColNr].ActualWidth;

                        double diff_X = col_x - mouse_x - adjust_Offset_X;

 

                       

 

                        if (adjust_ColNr < GridMain.ColumnDefinitions.Count)

                        {

                            GridMain.ColumnDefinitions[adjust_ColNr + 1].Width = new GridLength(GridMain.ColumnDefinitions[adjust_ColNr + 1].ActualWidth, GridUnitType.Star);

                        }

 

                        //--< set Left Col >--

                        ColumnDefinition colDef = GridMain.ColumnDefinitions[adjust_ColNr ];

                        double new_Width = colDef.ActualWidth - diff_X;

 

                        //< set >

                        if (new_Width < 0) return;

                        colDef.Width = new GridLength(new_Width);

                        //</ set >

                        //--< set Left Col >--

                        //----</ Adjust to Right >----

                    }

                    //-------------</ adjust Column >--------------

 

 

                    //-------------< adjust Rows >--------------

                    //--< get row_y >--

                    double row_y = 0;

                    for (int i = 0; i < adjust_RowNr; i++)

                    {

                        row_y += GridMain.RowDefinitions[i].ActualHeight;

                    }

                    //--</ get row_y >--

 

 

                    if (adjust_Dir_Rows == (int)Adjust_To_Row.Top)

                    {

                        //----< Adjust to Top >----

                        //< check >

                        if (adjust_RowNr < 1) return;

                        //</ check >

 

                        double diff_Y = row_y - mouse_y + adjust_Offset_Y;

 

                        //< prepare follower >

                        GridMain.RowDefinitions[adjust_RowNr].Height  = new GridLength(GridMain.RowDefinitions[adjust_RowNr].ActualHeight , GridUnitType.Star);

                        //</ prepare follower >

 

                        //--< set Top Row >--

                        RowDefinition rowDef = GridMain.RowDefinitions[adjust_RowNr - 1];

                        double new_Height = rowDef.ActualHeight - diff_Y;

 

                        //< set >

                        if (new_Height < 0) return;

                        rowDef.Height  = new GridLength(new_Height);

                        //</ set >

                        //--< set Top Row >--

                        //----</ Adjust to Top >----

                    }

                    else

                    {                       

                        //----< Adjust to Bottom >----

                        //< check >

                        if (adjust_RowNr < 0) return;

                        if (adjust_RowNr >= GridMain.RowDefinitions.Count-1) return;

                        //</ check >

 

                        row_y += GridMain.RowDefinitions[adjust_RowNr].ActualHeight;

                        double diff_Y = row_y - mouse_y - adjust_Offset_Y;

 

                        //< prepare follower >

                        if(adjust_RowNr<GridMain.RowDefinitions.Count)

                        {

                            GridMain.RowDefinitions[adjust_RowNr+1].Height = new GridLength(GridMain.RowDefinitions[adjust_RowNr+1].ActualHeight, GridUnitType.Star);

                        }

                        //</ prepare follower >

 

                        //--< set Bottom Row >--

                        RowDefinition rowDef = GridMain.RowDefinitions[adjust_RowNr ];

                        double new_Height = rowDef.ActualHeight - diff_Y;

 

                        //< set >

                        if (new_Height < 0) return;

                        rowDef.Height = new GridLength(new_Height);

                        //</ set >

                        //--< set Bottom Row >--

                        //----</ Adjust to Bottom >----

 

                    }

                    //-------------</ adjust Rows >--------------

 

 

 

                    //------</ MouseButtonState.Pressed >------

                }

            }

            else

            {

                //if (adjust_ColNr == -1)

                //    //throw ;

                //    ;

            }

            //-------------</ adjust_Cols_And_Rows() >-------------

        }

        #endregion /mouse dragging

    }

}

 

 

 

UserControl: UcPanel

Im UserControl UcPanel befindet sich die Grid-Zelle.

Diese wiederum lädt den Inhalt, welcher in der Zelle angezeigt werden soll und bei Bedarf das unterhalb liegendende Usercontrol UcAdjuster.

 

 

<UserControl x:Class="test_wpf_DynaGrid._UserControls.UcPanel"

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

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

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

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

             xmlns:local="clr-namespace:test_wpf_DynaGrid._UserControls"

             mc:Ignorable="d"

             d:DesignHeight="300" d:DesignWidth="300">

    <Grid x:Name="gridPanel" MouseEnter="gridPanel_MouseEnter" MouseLeave="gridPanel_MouseLeave" >

        <Border x:Name="pnlBackground" Background="#EEEEEE" Margin="1" BorderThickness="0" BorderBrush="DarkGray"  ></Border>

        <TextBlock x:Name="lblCaption" VerticalAlignment="Center"  HorizontalAlignment="Center" TextAlignment="Center"   Width="100"></TextBlock>

        <local:UcAdjuster x:Name="ucAdjuster" Visibility="Hidden" > </local:UcAdjuster>

    </Grid>

</UserControl>

 

 

C# Code des UserControls UcPanel

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._UserControls

{

    /// <summary>

    /// Interaction logic for UcPanel.xaml

    /// </summary>

    public partial class UcPanel : UserControl

    {

 

 

        public string Caption

        {

            get { return lblCaption.Text ; }

            set { lblCaption.Text= value; }

        }

 

       

        public void hide_Adjuster()

        {

            pnlBackground.BorderThickness = new Thickness(0);

            ucAdjuster.Visibility = Visibility.Hidden ;

        }

 

        public UcPanel()

        {

            InitializeComponent();

        }

 

        private void gridPanel_MouseEnter(object sender, MouseEventArgs e)

        {

 

            //< get main >

            Grid gridMain = this.Parent as Grid;

            MainWindow main = gridMain.Parent as MainWindow;

            if (main.isAdjusting == true) return ;

            //</ get main >

 

 

            pnlBackground.BorderThickness = new Thickness(1);

            ucAdjuster.Visibility = Visibility.Visible;

        }

 

        private void gridPanel_MouseLeave(object sender, MouseEventArgs e)

        {

            //< get main >

            Grid gridMain = this.Parent as Grid;

            MainWindow main = gridMain.Parent as MainWindow;

            if (main.isAdjusting == true) return;

            //</ get main >

 

            pnlBackground.BorderThickness = new Thickness(0);

            ucAdjuster.Visibility = Visibility.Hidden ;

        }

    }

}

 

 

 

Usercontrol: UCAdjuster

 

Das Usercontrol UcAdjuster stellt den Veränderungsrahmen dar, welcher sichtbar wird, sobald man sich in eine Gridzelle bewegt.

In dem Usercontrol befinden sich die 4 Ecken, welche jeweil einen Event auslösen beim Mouse_Down

 

 

XAml Code des Adjuster User-Controls

<UserControl x:Class="test_wpf_DynaGrid._UserControls.UcAdjuster"

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

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

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

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

             xmlns:local="clr-namespace:test_wpf_DynaGrid._UserControls"

             mc:Ignorable="d"

             d:DesignHeight="300" d:DesignWidth="300">

    <Grid x:Name="gridAdjuster" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"    >

 

        <Border  x:Name="recLeftTop" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="10" Height="10" Background ="Gray" Opacity="0.2" MouseDown="recLeftTop_MouseDown"  >

        </Border>

        <Border  x:Name="recRightTop" HorizontalAlignment="Right" VerticalAlignment="Top" Width="10" Height="10" Background ="Gray" Opacity="0.2" MouseDown="recRightTop_MouseDown">

        </Border>

        <Border  x:Name="recLeftBottom" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="10" Height="10" Background ="Gray" Opacity="0.2" MouseDown="recLeftBottom_MouseDown">

        </Border>

        <Border  x:Name="recRightBottom" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="10" Height="10" Background ="Gray" Opacity="0.2" MouseDown="recRightBottom_MouseDown">

        </Border>

    </Grid>

</UserControl>

 

 

 

C# Code des Adjusters

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._UserControls

{

    /// <summary>

    /// Interaction logic for UcAdjuster.xaml

    /// </summary>

    public partial class UcAdjuster : UserControl

    {

        #region EdgeTypes

        //==========================< Region: CropEdges >==========================

        public int edgeType = 0;

 

        enum EdgeTypes

        {

            TopLeft = 1,

            TopRight = 2,

            BottomLeft = 3,

            BottomRight = 4

        }

 

 

        private void recRightBottom_MouseDown(object sender, MouseButtonEventArgs e)

        {

            edgeType = (int)EdgeTypes.BottomRight;

            set_IsAdjusting(sender,e);

        }

 

        private void recLeftBottom_MouseDown(object sender, MouseButtonEventArgs e)

        {

            edgeType = (int)EdgeTypes.BottomLeft;

            set_IsAdjusting(sender, e);

        }

 

        private void recRightTop_MouseDown(object sender, MouseButtonEventArgs e)

        {

            edgeType = (int)EdgeTypes.TopRight;

            set_IsAdjusting(sender, e);

        }

 

        private void recLeftTop_MouseDown(object sender, MouseButtonEventArgs e)

        {

            edgeType = (int)EdgeTypes.TopLeft;

            set_IsAdjusting(sender, e);

        }

 

      

 

 

 

 

 

        //==========================< Region: CropEdges >==========================

        #endregion

 

        public UcAdjuster()

        {

            InitializeComponent();

        }

       

 

      

 

 

        #region Mouse-Events

        //==========================< Region: Mouse_Events >==========================

       

       

        private void set_IsAdjusting(object sender, MouseButtonEventArgs e)

        {

            //-------------< set_IsAdjusting() >-------------

            //< set main >

            Grid gridPanel = this.Parent as Grid;

            UcPanel panel = gridPanel.Parent as UcPanel;

            Grid gridMain = panel.Parent as Grid;

            MainWindow main = gridMain.Parent as MainWindow;

            main.isAdjusting = true;

            main.adjust_EdgeType = edgeType;

            //</ set main >

 

            //----< set row col >----

            int row = Grid.GetRow(panel);

            int col = Grid.GetColumn(panel);

            main.adjust_ColNr = col;

            main.adjust_RowNr = row;

            //----</ set row col >----

 

            Border rect = sender as Border;

            Point point_Offset = e.GetPosition(rect);

            if (edgeType == (int)EdgeTypes.TopLeft)

            {

                main.adjust_Offset_X = point_Offset.X;

                main.adjust_Offset_X = point_Offset.Y;

            }

            else if (edgeType == (int)EdgeTypes.TopRight)

            {

                main.adjust_Offset_X = rect.ActualWidth  -point_Offset.X;

                main.adjust_Offset_X = point_Offset.Y;

            }

            if (edgeType == (int)EdgeTypes.BottomLeft)

            {

                main.adjust_Offset_X = point_Offset.X;

                main.adjust_Offset_X = rect.ActualHeight - point_Offset.Y;

            }

            else if (edgeType == (int)EdgeTypes.BottomRight)

            {

                main.adjust_Offset_X = rect.ActualWidth - point_Offset.X;

                main.adjust_Offset_X = rect.ActualHeight- point_Offset.Y;

            }

            //-------------</ set_IsAdjusting() >-------------

        }

 

        //==========================</ Region: Mouse_Events >==========================

        #endregion

 

        //private void gridAdjuster_MouseUp(object sender, MouseButtonEventArgs e)

        //{

        //    //< bubble to main event >

        //    Grid gridPanel = this.Parent as Grid;

        //    UcPanel panel = gridPanel.Parent as UcPanel;

        //    Grid gridMain = panel.Parent as Grid;

        //    MainWindow main = gridMain.Parent as MainWindow;

        //    main.BaseGrid_MouseUp(sender, e);

        //    //</ bubble to main event >

        //}

    }

}

 

 

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