locked
How to use DataGridCheckBoxColumn ? RRS feed

  • General discussion

  • Hello Guys !!

    How can I use this DataGridCheckBoxColumn ?

    It is possible create a DataGrid with a lot of CheckBox in the xaml ? or only using the Code Behind ?

    I have this code but no works well:

    — I’m using ObservableCollection.

    public class atualizaDataGridChild : INotifyPropertyChanged
    {
        #region propriedades Atualizagrid
     
        public string coluna1;
        public string Coluna1
        {
            get { return this.coluna1; }
     
            set
            {
                if (value != this.coluna1)
                {
                    this.coluna1 = value;
                    this.NotifyPropertyChanged("Coluna1");
                }
            }
        }
     
        public CheckBox coluna2;
        public CheckBox Coluna2
        {
            get
            {
                return this.coluna2;
            }
     
            set
            {
                if (value != this.coluna2)
                {
                    this.coluna2 = value;
                    this.NotifyPropertyChanged("Coluna2");
                }
            }
        }
     
        public CheckBox coluna3;
        public CheckBox Coluna3
        {
            get
            {
                return this.coluna3;
            }
     
            set
            {
                if (value != this.coluna3)
                {
                    this.coluna3 = value;
                    this.NotifyPropertyChanged("Coluna3");
                }
            }
        }
     
        public CheckBox coluna4;
        public CheckBox Coluna4
        {
            get
            {
                return this.coluna4;
            }
     
            set
            {
                if (value != this.coluna4)
                {
                    this.coluna4 = value;
                    this.NotifyPropertyChanged("Coluna4");
                }
            }
        }
     
        public CheckBox coluna5;
        public CheckBox Coluna5
        {
            get
            {
                return this.coluna5;
            }
     
            set
            {
                if (value != this.coluna5)
                {
                    this.coluna5 = value;
                    this.NotifyPropertyChanged("Coluna5");
                }
            }
        }
        #endregion
     
        #region NotifyPropertyChanged
     
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

    ObservableCollection<atualizaDataGridChild> _atualizaGridChild = new ObservableCollection<atualizaDataGridChild>();
    

    void CarregaGrid()
            {
                ListViewDataGrid.ItemsSource = null;
                ListViewDataGrid.Columns.Clear();
                _atualizaGridChild.Clear();
     
                string[] itens = new string[22] { "Unidade", "Recepcao", "Estacionamento", "Pessoa",
                "Veiculo", "Identificador", "Prestadora", "Prestador", "Servico", "Tarefa", "Visitante",
                "Correspondencia", "Ocorrencia", "Locacao", "Supervisao", "Relatorio", "Estatistica",
                "Manutencao", "Configuracao", "Site", "Operador", "Grupo"};
     
                C1.Silverlight.DataGrid.DataGridTextColumn TituloTextColumn = new C1.Silverlight.DataGrid.DataGridTextColumn();
     
                C1.Silverlight.DataGrid.DataGridCheckBoxColumn IncluirCheckBoxColumn = new C1.Silverlight.DataGrid.DataGridCheckBoxColumn();          
     
                C1.Silverlight.DataGrid.DataGridCheckBoxColumn AlterarCheckBoxColumn = new C1.Silverlight.DataGrid.DataGridCheckBoxColumn();
                AlterarCheckBoxColumn.Header = "Alterar";
     
                C1.Silverlight.DataGrid.DataGridCheckBoxColumn ExcluirCheckBoxColumn = new C1.Silverlight.DataGrid.DataGridCheckBoxColumn();
     
                C1.Silverlight.DataGrid.DataGridCheckBoxColumn VisualizarCheckBoxColumn = new C1.Silverlight.DataGrid.DataGridCheckBoxColumn();
     
                foreach (var item in itens)
                {
                    CheckBox checkBox = new CheckBox();
                    checkBox.Tag = item + "CheckBox";
     
                    _atualizaGridChild.Add(new atualizaDataGridChild()
                    {
                        Coluna1 = item.ToString(),
                        Coluna2 = checkBox,
                        Coluna3 = checkBox,
                        Coluna4 = checkBox,
                        Coluna5 = checkBox,
                    });
                }
     
                TituloTextColumn.Binding = new System.Windows.Data.Binding("Coluna1");
                IncluirCheckBoxColumn.Binding = new System.Windows.Data.Binding("Coluna2");
                AlterarCheckBoxColumn.Binding = new System.Windows.Data.Binding("Coluna3");
                ExcluirCheckBoxColumn.Binding = new System.Windows.Data.Binding("Coluna4");
                VisualizarCheckBoxColumn.Binding = new System.Windows.Data.Binding("Coluna5");
     
                ListViewDataGrid.Columns.Add(TituloTextColumn);
                ListViewDataGrid.Columns.Add(IncluirCheckBoxColumn);
                ListViewDataGrid.Columns.Add(AlterarCheckBoxColumn);
                ListViewDataGrid.Columns.Add(ExcluirCheckBoxColumn);
                ListViewDataGrid.Columns.Add(VisualizarCheckBoxColumn);
     
                ListViewDataGrid.ItemsSource = _atualizaGridChild;
            }

    I want to reproduce this image…

    Thank advance

    Monday, October 1, 2012 9:48 AM

All replies

  • Binding mode should be two way; See the below example

    Binding binding = new Binding("Coluna1");
    binding.Mode = BindingMode.TwoWay;
    TitiloTextColumn.Binding = binding;


     

     

    Tuesday, October 2, 2012 3:10 AM
  • Hi,
    I am not sure what is the issue you are facing.  But I believe that you can use below code to create the page you shown in the picture.
    Here I guessed you have 4 checkbox columns  datgrid. If you want to make this dynamic, we need to make some changes in the code.
    
    
    The XAML:
    
    <UserControl x:Class="DataGridCheckBoxColumn.MainPage"
        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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions >
                <RowDefinition Height="*">
                    
                </RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions >
                <ColumnDefinition Width="*">
                    
                </ColumnDefinition>
            </Grid.ColumnDefinitions>
    
            <sdk:DataGrid AutoGenerateColumns="False"  x:Name="dgActualization" >
                <sdk:DataGrid.Columns >
                    <sdk:DataGridTextColumn  Binding="{Binding Coluna1}"  ></sdk:DataGridTextColumn>
                    <sdk:DataGridTemplateColumn >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Width="Auto">
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="25"/>
                                         <ColumnDefinition Width="*"/>
    
                                       
                                    </Grid.ColumnDefinitions>
                                    <CheckBox IsChecked="{Binding Coluna2.IsChecked, Mode=TwoWay}" Grid.Column="0" Margin="3"></CheckBox>
                                    <TextBox Text="{Binding Coluna2.Title}" Grid.Column="1" Margin="5" Padding="3"></TextBox>
                                    
                                </Grid>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
    
                    <sdk:DataGridTemplateColumn >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Width="Auto">
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="25"/>
                                        <ColumnDefinition Width="*"/>
    
    
                                    </Grid.ColumnDefinitions>
                                    <CheckBox IsChecked="{Binding Coluna3.IsChecked, Mode=TwoWay}" Grid.Column="0" Margin="3"></CheckBox>
                                    <TextBox Text="{Binding Coluna3.Title}" Grid.Column="1" Margin="5" Padding="3"></TextBox>
                                    
                                </Grid>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
    
                    <sdk:DataGridTemplateColumn >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Width="Auto">
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="25"/>
                                        <ColumnDefinition Width="*"/>
    
    
                                    </Grid.ColumnDefinitions>
    
                                    <CheckBox IsChecked="{Binding Coluna4.IsChecked, Mode=TwoWay}" Grid.Column="0" Margin="3"></CheckBox>
                                    <TextBox Text="{Binding Coluna4.Title}" Grid.Column="1" Margin="5" Padding="3"></TextBox>
                                </Grid>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn >
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Width="Auto">
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="25"/>
                                        <ColumnDefinition Width="*"/>
    
    
                                    </Grid.ColumnDefinitions>
    
                                    <CheckBox  IsChecked="{Binding Coluna5.IsChecked, Mode=TwoWay}" Grid.Column="0" Margin="3"></CheckBox>
                                    <TextBox Text="{Binding Coluna5.Title}" Grid.Column="1" Margin="5" Padding="3"></TextBox>
                                </Grid>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                  
                </sdk:DataGrid.Columns>
    
            </sdk:DataGrid>
            
        </Grid>
    </UserControl>

    The code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel  ;
     
    namespace DataGridCheckBoxColumn
    {
        public partial class MainPage : UserControl
        {
            string[] itens = new string[22] { "Unidade""Recepcao""Estacionamento""Pessoa",
                "Veiculo""Identificador""Prestadora""Prestador""Servico""Tarefa""Visitante",
                "Correspondencia""Ocorrencia""Locacao""Supervisao""Relatorio""Estatistica",
                "Manutencao""Configuracao""Site""Operador""Grupo"};
            private ObservableCollection<atualizaDataGridChild> atualizaDataGridChildSource;
     
            public ObservableCollection<atualizaDataGridChild> AtualizaDataGridChildSource
            {
                get { return atualizaDataGridChildSource; }
                set { atualizaDataGridChildSource = value; }
            }
     
            public MainPage()
            {
                InitializeComponent();
                
                AtualizaDataGridChildSource=new ObservableCollection<atualizaDataGridChild> ();
     
                foreach (var item in itens)
                {
                  AtualizaDataGridChildSource.Add (  new atualizaDataGridChild()
                    {
                        Coluna1 = item.ToString(),
                        Coluna2 = new MyCheckBox (){Title ="Induir",IsChecked =false},
                        Coluna3 = new MyCheckBox (){Title ="Alterar",IsChecked =false},
                        Coluna4 = new MyCheckBox (){Title ="Exduir",IsChecked =false},
                        Coluna5 = new MyCheckBox (){Title ="Visualizar",IsChecked =false},
                    });
     
                }
     
                dgActualization.ItemsSource = AtualizaDataGridChildSource;
     
            }
        }
     
        public class atualizaDataGridChild : INotifyPropertyChanged
        {
     
            public string coluna1;
            public string Coluna1
            {
                get { return this.coluna1; }
     
                set
                {
                    if (value != this.coluna1)
                    {
                        this.coluna1 = value;
                        this.NotifyPropertyChanged("Coluna1");
                    }
                }
            }
     
      public MyCheckBox coluna2;
        public MyCheckBox Coluna2
        {
            get
            {
                return this.coluna2;
            }
     
            set
            {
                if (value != this.coluna2)
                {
                    this.coluna2 = value;
                    this.NotifyPropertyChanged("Coluna2");
                }
            }
        }
     
        public MyCheckBox coluna3;
        public MyCheckBox Coluna3
        {
            get
            {
                return this.coluna3;
            }
     
            set
            {
                if (value != this.coluna3)
                {
                    this.coluna3 = value;
                    this.NotifyPropertyChanged("Coluna3");
                }
            }
        }
     
        public MyCheckBox coluna4;
        public MyCheckBox Coluna4
        {
            get
            {
                return this.coluna4;
            }
     
            set
            {
                if (value != this.coluna4)
                {
                    this.coluna4 = value;
                    this.NotifyPropertyChanged("Coluna4");
                }
            }
        }
     
        public MyCheckBox coluna5;
        public MyCheckBox Coluna5
        {
            get
            {
                return this.coluna5;
            }
     
            set
            {
                if (value != this.coluna5)
                {
                    this.coluna5 = value;
                    this.NotifyPropertyChanged("Coluna5");
                }
            }
        }
     
     
            #region NotifyPropertyChanged
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
            }
            #endregion
        }
     
        public class MyCheckBox:INotifyPropertyChanged
        {
            private string title;
     
             public string Title
            {
                get { return this.title; }
     
                set
                {
                    if (value != this.title)
                    {
                        this.title = value;
                        this.NotifyPropertyChanged("Title");
                    }
                }
            }
     
     
             private bool isChecked;
     
             public bool IsChecked
             {
                 get { return isChecked; }
                 set { isChecked = value;
                 this.NotifyPropertyChanged("IsChecked");
                 }
             }
     
            #region NotifyPropertyChanged
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
            }
            #endregion
        }
    }
    

    Tuesday, October 2, 2012 2:56 PM