<UserControl x:Class="TestGrid.GridControl" 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" mc:Ignorable="d" > <StackPanel > <DataGrid GridLinesVisibility="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" MinHeight="100" MaxHeight="300" Width="Auto" Name="McDataGrid" Grid.Row="1" Grid.Column="2" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Path=TestData}" DataContext="{Binding}" > <DataGrid.Columns> <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"> <DataGridCheckBoxColumn.Header> <CheckBox Name="selectallchkbox3" IsChecked="{Binding DataContext.SelectAll1, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, UpdateSourceTrigger=PropertyChanged }"/> </DataGridCheckBoxColumn.Header> </DataGridCheckBoxColumn> <DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=OneWay}" IsReadOnly="True" Width="250"/> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Path=InstruQuantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </StackPanel> </UserControl>
namespace ViewModel { public class Authors: ViewModel.ViewProperty { public Authors() { LoadCollectionData(); } private DataTable authors = new DataTable(); public DataTable TestData { get { return authors; } } public void LoadCollectionData() { DataColumn col = new DataColumn(); col.ColumnName = "Name"; col.DataType = typeof(string); authors.Columns.Add(col); DataColumn col2 = new DataColumn(); col2.ColumnName = "InstruQuantity"; col2.DataType = typeof(string); authors.Columns.Add(col2); DataColumn colcheck = new DataColumn("IsSelected", typeof(bool)); authors.Columns.Add(colcheck); DataRow row1 = authors.NewRow(); row1[0] = "Ganga"; row1[1] = "10"; authors.Rows.Add(row1); DataRow row2 = authors.NewRow(); row2[0] = "Bhagya"; row2[1] = "20"; authors.Rows.Add(row2); } private bool _SelectAll; public bool SelectAll1 { get { return _SelectAll; } set { _SelectAll = value; OnPropertyChanged("SelectAll1"); SelectAll(); } } public void SelectAll() { if (SelectAll1 == true) { foreach (DataRow row in authors.Rows) { row["IsSelected"] = true; } } else { foreach (DataRow row in authors.Rows) { row["IsSelected"] = false; } } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)