wpf dataGrid

View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MyWPFCustomControls"
                    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
                    >
    <Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle x:Name="Border" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SnapsToDevicePixels="True"/>
                        <Polygon x:Name="Arrow" Fill="Black" HorizontalAlignment="Right" Margin="8,8,3,3" Opacity="0.15" Points="0,10 10,10 10,0" Stretch="Uniform" VerticalAlignment="Bottom"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style  TargetType="{x:Type local:CustomDataGrid}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FF688CAF"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="CellStyle" >
            <Setter.Value>
                <Style  TargetType="{x:Type DataGridCell}" >
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataContext.IsSelected,RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
                            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        </DataTrigger>
                        <!--<Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>-->
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomDataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
       
            
       
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <SolidColorBrush x:Key="{x:Static DataGrid.FocusBorderBrushKey}" Color="#FF000000"/>
    
    <Style x:Key="CustomDataGridCellStyle" TargetType="{x:Type DataGridCell}" >
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding DataContext.IsSelected,RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </DataTrigger>
            <!--<Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>-->
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="8"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeWE"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" ThemeColor="NormalColor">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Microsoft_Windows_Themes:DataGridHeaderBorder>
                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <BooleanToVisibilityConverter x:Key="bool2VisibilityConverter"/>
    <Style x:Key="RowHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Height" Value="8"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeNS"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <Grid>
                        <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" IsSelected="{TemplateBinding IsRowSelected}" Orientation="Horizontal" Padding="{TemplateBinding Padding}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" ThemeColor="NormalColor">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"  />
                                <Control SnapsToDevicePixels="false" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility="{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
                            </StackPanel>
                        </Microsoft_Windows_Themes:DataGridHeaderBorder>
                        
                        <Thumb x:Name="PART_TopHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Top"/>
                        <Thumb x:Name="PART_BottomHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Bottom"/>
                    </Grid>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="1" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace MyWPFCustomControls
{
    public class CustomDataGrid : DataGrid
    {

        private bool SelectAll;
        private bool IsClicked;
        private bool IsSelectionChanged;

        public CustomDataGrid()
        {
            this.CanUserResizeRows = false;
            this.CanUserResizeColumns = false;
            this.SelectionUnit = DataGridSelectionUnit.FullRow;
            this.SelectionChanged += CustomDataGrid_SelectionChanged;
            this.MouseLeftButtonUp += CustomDataGrid_MouseLeftButtonUp;
            this.LoadingRow += CustomDataGrid_LoadingRow;
            IsClicked = true;
            SelectAll = true;
            DataGridTemplateColumn tc = new DataGridTemplateColumn();
            //DataTemplate dt = new DataTemplate();
            tc.CanUserSort = true;
            tc.CanUserResize = false;
            tc.CanUserReorder = false;
            tc.SortMemberPath = "IsSelected";
            

            CheckBox cb = new CheckBox() { Margin = new Thickness(8, 0, 0, 0) };

            cb.Click += new RoutedEventHandler(cb_Click);
            tc.Header = cb;


            DataTemplate dt1 = new DataTemplate();

            FrameworkElementFactory fef1 = new FrameworkElementFactory(typeof(CheckBox));

            Binding binding1 = new Binding();
            binding1.Path = new PropertyPath("IsSelected");
            binding1.Mode = BindingMode.TwoWay;
            binding1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            fef1.SetBinding(CheckBox.IsCheckedProperty, binding1);
            fef1.SetValue(CheckBox.MarginProperty, new Thickness(10, 0, 0, 0));
            //fef1.SetValue(CheckBox.NameProperty, "cBoxSelected");

            fef1.AddHandler(CheckBox.ClickEvent, (RoutedEventHandler)cBoxSelected_Click_1, true);

            dt1.VisualTree = fef1;

            tc.CellTemplate = dt1;
           
            this.Columns.Add(tc);

        }


        static CustomDataGrid()
        {
            //  DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomDataGrid), new FrameworkPropertyMetadata(typeof(CustomDataGrid)));
        }


        void CustomDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            string index =( e.Row.GetIndex() + 1)+"";
            Grid g = new Grid() { Width=25,VerticalAlignment=VerticalAlignment.Stretch,HorizontalAlignment=HorizontalAlignment.Stretch,Background=new SolidColorBrush(Colors.Red) };

            g.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(g_PreviewMouseLeftButtonDown);
            TextBlock t = new TextBlock() { Text = index,Margin=new Thickness(10,0,0,0), VerticalAlignment = VerticalAlignment.Stretch,HorizontalAlignment=HorizontalAlignment.Stretch};
           // t.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(t_PreviewMouseLeftButtonDown);
            g.Children.Add(t);

            e.Row.Header = g;

            
        }

        void t_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            
        }

        void g_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.SelectedItem = null;
            IsClicked = false;
        }

        

        
        void cb_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

            CheckBox header = tempColumn.Header as CheckBox;


            foreach (var item in this.Items)
            {
                if (item.GetType().GetProperty("IsSelected") != null)
                {
                    if (cb.IsChecked.GetValueOrDefault())
                    {
                        item.GetType().GetProperty("IsSelected").SetValue(item, true, null);
                    }
                    else
                    {
                        item.GetType().GetProperty("IsSelected").SetValue(item, false, null);
                    }

                }


            }




        }

        void CustomDataGrid_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!IsSelectionChanged)
            {
                //foreach (var item in dataGrid1.SelectedItems)
                //{

                int count = this.SelectedItems.Count;
                if (count > 1)
                {
                    foreach (var item in this.SelectedItems)
                    {
                        if (item != null)
                        {
                            if (item.GetType().GetProperty("IsSelected") != null)
                            {
                                if ((bool)item.GetType().GetProperty("IsSelected").GetValue(item, null))
                                {
                                    item.GetType().GetProperty("IsSelected").SetValue(item, SelectAll, null);

                                    DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

                                    CheckBox header = tempColumn.Header as CheckBox;
                                    header.IsChecked = false;
                                }
                                else
                                {
                                    item.GetType().GetProperty("IsSelected").SetValue(item, SelectAll, null);
                                    CheckIsCheckedAll();
                                }

                            }
                            //DataGridTemplateColumn templeColumn = this.Columns[0] as DataGridTemplateColumn;
                            //FrameworkElement fwElement = this.Columns[0].GetCellContent(item);
                            //CheckBox objChk = templeColumn.CellTemplate.FindName("cBoxSelected", fwElement) as CheckBox;

                            //objChk.IsChecked = SelectAll;
                            //if (!SelectAll)
                            //{
                            //    this.SelectedItem = null;
                            //}

                        }

                    }
                    if (SelectAll)
                    {

                        SelectAll = false;
                    }
                    else
                    {
                        SelectAll = true;
                    }
                    IsClicked = true;
                }
                else
                {
                    var item = this.SelectedItem;
                    if (item != null)
                    {
                        if (item.GetType().GetProperty("IsSelected") != null)
                        {
                            if ((bool)item.GetType().GetProperty("IsSelected").GetValue(item, null))
                            {
                                item.GetType().GetProperty("IsSelected").SetValue(item, false, null);
                                this.SelectedItem = null;
                                item.GetType().GetProperty("IsSelected").SetValue(item, false, null);
                                DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

                                CheckBox header = tempColumn.Header as CheckBox;
                                header.IsChecked = false;
                            }
                            else
                            {
                                item.GetType().GetProperty("IsSelected").SetValue(item, true, null);
                                CheckIsCheckedAll();
                            }

                        }

                        IsClicked = true;
                    }
                }


                //    //}
            }
            else
            {
                IsSelectionChanged = false;

            }
        }




        private void cBoxSelected_Click_1(object sender, RoutedEventArgs e)
        {
            // IsSelectionChanged = true;
            CheckBox cb = sender as CheckBox;
            if (cb.IsChecked.GetValueOrDefault())
            {
                CheckIsCheckedAll();

            }
            else
            {
                DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

                CheckBox header = tempColumn.Header as CheckBox;
                header.IsChecked = false;
                this.SelectedItem = null;
            }

            IsClicked = true;

        }
        void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!IsClicked)
            {

                var item = this.SelectedItem;
                if (item != null)
                {

                    if (item.GetType().GetProperty("IsSelected") != null)
                    {
                        if ((bool)item.GetType().GetProperty("IsSelected").GetValue(item, null))
                        {
                            item.GetType().GetProperty("IsSelected").SetValue(item, false, null);
                            DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

                            CheckBox header = tempColumn.Header as CheckBox;
                            header.IsChecked = false;

                        }
                        else
                        {
                            item.GetType().GetProperty("IsSelected").SetValue(item, true, null);
                            CheckIsCheckedAll();
                        }

                    }
                    //DataGridTemplateColumn templeColumn = this.Columns[0] as DataGridTemplateColumn;

                    //FrameworkElement fwElement = this.Columns[0].GetCellContent(item);

                    //CheckBox objChk = templeColumn.CellTemplate.FindName("cBoxSelected", fwElement) as CheckBox;

                    //if (objChk.IsChecked == true)
                    //{
                    //    objChk.IsChecked = false;
                    //}
                    //else
                    //    objChk.IsChecked = true;

                    IsSelectionChanged = true;
                }

            }
            else
            {
                IsClicked = false;
            }
        }

        private void CheckIsCheckedAll()
        {
            int count = 0;
            foreach (var item in this.Items)
            {
                if (item.GetType().GetProperty("IsSelected") != null && ((bool)(item.GetType().GetProperty("IsSelected").GetValue(item, null))) == true)
                {
                    count++;
                }

            }
            if (count == Items.Count)
            {
                DataGridTemplateColumn tempColumn = this.Columns[0] as DataGridTemplateColumn;

                CheckBox header = tempColumn.Header as CheckBox;
                header.IsChecked = true;
            }
        }

    }
}

 

posted @ 2013-04-16 23:57  法的空间  阅读(395)  评论(0编辑  收藏  举报