Understanding WPF Layout Panels: Easy Examples of Grid, StackPanel, WrapPanel, DockPanel, Canvas & UniformGrid

If you are just starting out with WPF , layouts are something you must understand very clearly, because they control how every control such as button, textbox, etc. appears on the screen. ⭐ What Are Layout Panels? Think of layout panels like containers or boxes that help you arrange your UI elements . Just like when you pack a suitcase, you organize things in different sections — WPF uses layout panels to organize controls properly. They decide: where controls appear, how they resize, how they adapt when the window grows or shrinks. 📌 Let’s Learn Panels One by One (with real examples) 🟦 1. StackPanel — arrange controls in a line StackPanel arranges items one after another, either top to bottom or left to right. Let me show you a simple example. ✔ Example: Vertical StackPanel <StackPanel Orientation="Vertical">     <Button Content="Save" Width="100"/>     <Button Content="Edit" Width="100"/>     <Button Conte...

Multiple ways to handle Checked/Unchecked event in wpf using MVVM

In this post i am trying to explain multiple way to handle Checked/Unchecked event in wpf using MVVM

First let's create a view which contains multiple CheckBox. 

All CheckBox will handle the Checked/Unchecked event differently. 


CheckBoxEx.xaml


<Window x:Class="PrismApp.Views.CheckBoxEx"

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

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

             xmlns:prism="http://prismlibrary.com/"

             prism:ViewModelLocator.AutoWireViewModel="True">

    <StackPanel >

        <CheckBox Content="Bind Command by passing CheckBox as CommandParameter but set command parameter type is object" HorizontalAlignment="Left" VerticalAlignment="Center"  Name="chkObj"

                  Command="{Binding CheckedChangedCommand}" CommandParameter="{Binding ElementName=chkObj}"/>

        <CheckBox Content="Bind Command by passing CheckBox as CommandParameter" HorizontalAlignment="Left" VerticalAlignment="Center"  Name="chk"

                  Command="{Binding CheckedByCheckBoxCommand}" CommandParameter="{Binding ElementName=chk}"/>

        <CheckBox Content="Bind Command by passing CheckBox IsChecked property as CommandParameter" HorizontalAlignment="Left" VerticalAlignment="Center"  Name="chkbx"

                  Command="{Binding CheckedByIsCheckedPropertyCommand}" CommandParameter="{Binding ElementName=chkbx,Path=IsChecked}"/>

        <CheckBox Content="Bind Command by passing DataContext as CommandParameter" HorizontalAlignment="Left" VerticalAlignment="Center"

                  Command="{Binding CheckedByDataContextCommand}" IsChecked="{Binding IsVmChecked}" CommandParameter="{Binding}"/>

        <CheckBox Content="By Binding IsChecked property of CheckBox with VM property" HorizontalAlignment="Left" VerticalAlignment="Center"

                 IsChecked="{Binding IsChecked}" />

    </StackPanel>

</Window>


CheckBoxEx.xaml.cs


    public partial class CheckBoxEx : Window

    {

        public CheckBoxEx()

        {

            InitializeComponent();

        }

    }



CheckBoxExViewModel.cs


using Prism.Commands;

using Prism.Mvvm;

using System.Windows.Controls;

using System.Windows;


    public class CheckBoxExViewModel : BindableBase

    {

        public CheckBoxExViewModel()

        {


        }


        #region 5 ways to handle the Checked/UnChecked of CheckBox using Command


        #region Way 1: When command parameter type is object need to cast

        private DelegateCommand<object> _checkedChangedCommand;

        public DelegateCommand<object> CheckedChangedCommand => (_checkedChangedCommand = new DelegateCommand<object>(CheckChanged));


        private void CheckChanged(object obj)

        {

            var chekbox = obj as CheckBox;

            MessageBox.Show($"Checkbox is checked (Bind Command by passing CheckBox as CommandParameter but set command parameter type is object) :{chekbox.IsChecked}");


        }

        #endregion


        #region Way 2: When command parameter type is CheckBox no need to cast

        private DelegateCommand<CheckBox> _checkedByCheckBoxCommand;

        public DelegateCommand<CheckBox> CheckedByCheckBoxCommand => (_checkedByCheckBoxCommand = new DelegateCommand<CheckBox>(CheckedByCheckBoxChanged));


        private void CheckedByCheckBoxChanged(CheckBox chekbox)

        {

            MessageBox.Show($"Checkbox is checked (Bind Command by passing CheckBox as CommandParameter) :{chekbox.IsChecked}");


        }

        #endregion


        #region Way 3:When command parameter type is CheckBox IsChecked property type

        private DelegateCommand<bool?> _checkedByIsCheckedPropertyCommand;

        public DelegateCommand<bool?> CheckedByIsCheckedPropertyCommand => (_checkedByIsCheckedPropertyCommand = new DelegateCommand<bool?>(CheckedByIsCheckedPropertyChange));

        private void CheckedByIsCheckedPropertyChange(bool? isChecked)

        {

            MessageBox.Show($"Checkbox is checked (Bind Command by passing CheckBox IsChecked property as CommandParameter):{isChecked}");


        }

        #endregion


        #region Way 4:When command parameter type is DataContext

        private bool _isVmChecked;


        public bool IsVmChecked

        {

            get { return _isVmChecked; }

            set

            {

                SetProperty(ref _isVmChecked, value);

            }

        }

        private DelegateCommand<CheckBoxExViewModel> _checkedByDataContextCommand;

        public DelegateCommand<CheckBoxExViewModel> CheckedByDataContextCommand => (_checkedByDataContextCommand = new DelegateCommand<CheckBoxExViewModel>(CheckedByDataContextChange));

        private void CheckedByDataContextChange(CheckBoxExViewModel datacontext)

        {

            MessageBox.Show($"Checkbox is checked (Bind Command by passing DataContext as CommandParameter):{datacontext.IsChecked}");


        }

        #endregion


        #region Way 5: By Binding IsChecked property of CheckBox

        private bool _isChecked;


        public bool IsChecked

        {

            get { return _isChecked; }

            set

            {

                SetProperty(ref _isChecked, value);

                MessageBox.Show($"Checkbox is checked (By Binding IsChecked property of CheckBox):{_isChecked}");

            }

        }

        #endregion


        #endregion

    }


Note: Way 1 and Way 2 are not recommended to use because we should not use any Views components in ViewModel.

Comments

Popular posts from this blog

Filter DataGrid and ListView in wpf using ICollectionView

Pagination of DataGrid in WPF using MVVM

How to Create TabControl using Prism Region