datagrid 第一列标题头里放CheckBox实现全选或全不选

时间:2022-03-14 10:25:56
如题. 我的写法是
<my:DataGrid> 
   <my:DataGrid.Columns>
        <my:DataGridTemplateColumn Width="350">
               <my:DataGridTemplateColumn.Header>
                    <DataTemplate>
                           <CheckBox x:Name="chkAllDel" >
                    <DataTemplate>
               </my:DataGridTemplateColumn.Header>
               <my:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                           <CheckBox x:Name="chkDel" >
                     </DataTemplate>
               </my:DataGridTemplateColumn.CellEditingTemplate>
          </my:DataGridTemplateColumn>
   </my:DataGrid.Columns>
</my:DataGrid>
可是标题的没有显示复选框  出现的是system.windows.datatemplate 我不知道哪错了. 谁来告诉我下

10 个解决方案

#1


我原来也遇到过这样的问题,也没有解决哦

#2



xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"



                <data:DataGridTextColumn Binding="{Binding A}" Header="hello">
                    <data:DataGridTextColumn.HeaderStyle>
                        <Style TargetType="dataprimitives:DataGridColumnHeader">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <CheckBox Content="{Binding B}" IsChecked="True" />
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </data:DataGridTextColumn.HeaderStyle>
                </data:DataGridTextColumn>


试试这个

#3


没人帮我吗?  我问的东西很长见吧.

#4


XAML

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid Name="dg" AutoGenerateColumns="False" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" ItemsSource="{Binding Collection}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridCheckBoxColumn Binding="{Binding IsChecked,Mode=TwoWay}" >
                    <sdk:DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="sdk:DataGridColumnHeader">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <CheckBox Content="全选" IsChecked="{Binding IsAllSelected,Mode=TwoWay}" />
                                            <CheckBox Content="全不选" IsChecked="{Binding IsNoneSelected,Mode=TwoWay}" />
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </sdk:DataGridCheckBoxColumn.HeaderStyle>
                </sdk:DataGridCheckBoxColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>

每一项的model

public class TestModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }
    }

datagrid的model

public class TestModelForDataGrid : INotifyPropertyChanged
    {
        public TestModelForDataGrid()
        {
            Collection = new ObservableCollection<TestModel>();
            IsAllSelected = false;
            IsNoneSelected = true;
            for (int i = 0; i < 10; i++)
                Collection.Add(new TestModel { IsChecked = false });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private bool isAllSelected;

        public bool IsAllSelected
        {
            get { return isAllSelected; }
            set 
            {
                if (isAllSelected != value)
                {
                    isAllSelected = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsAllSelected"));
                    if (value)
                    {
                        IsNoneSelected = false;
                        foreach (var c in Collection)
                            c.IsChecked = true;
                    }
                }
            }
        }

        private bool isNoneSelected;

        public bool IsNoneSelected
        {
            get { return isNoneSelected; }
            set
            {
                if (isNoneSelected != value)
                {
                    isNoneSelected = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsNoneSelected"));
                    if (value)
                    {
                        IsAllSelected = false;
                        foreach (var c in Collection)
                            c.IsChecked = false;
                    }
                }
            }
        }

        public ObservableCollection<TestModel> Collection { get; set; }
    }

界面初始化

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            TestModelForDataGrid model = new TestModelForDataGrid();
            dg.DataContext = model;
        }
    }

如果LZ还是坚持“我就是不想要model”,那我也没办法了,在SL/WPF中不用这种方式,慢慢受折磨吧,这样只是实现列头的全选全不选,点击下面每个checkbox改变列头的选择,还需要在testmodel中ischecked属性set函数中操作父model,这个可以通过在子model中加个父model的引用的方式实现

#5


网上有不少实例,楼主可以参考:

http://silverlightchina.net/html/tips/2009/1118/241.html

http://silverlightchina.net/html/developer/silverlight/2010/1012/2582.html

#6


 我已经实现了.  没用model  谢谢大家了

#7


希望这人的“执着”不要误导大家,本来好端端的隔离,就被这么弄混了,SL/WPF提供给我们绑定这座桥,难道是用来看的吗

#8


你把代码贴上来看看的

#9


我这人就这样,  不喜欢按照别人的东西走。 我没发现放一个model的好处, 也许你体会到了. MVVM 我也没体会出好处. 所以我都给拿掉了. 我想说的是,使用要用灵魂不要光用壳子. 如果硬套MVVM  什么的。 没有减轻工作反而增加了。 你没有必要说我写的如何, 我也不会去问你采用的什么. 我现在就是按照我的方式走. 实践出效果. 而走到现在,  我省去了100多个表的 model维护 .  这是我看见确实的好处

#10


MVVM是理念,更是灵魂,如果一个SL/WPF的开发还抱着从前那种观念作者ASP.Net/Winform时代相同的事情,那么还不如不要进入新的时代,就在那个原始的时代一直待着,这根本不是什么硬套,如果一意孤行,后面的苦涩请慢慢体会,不过不要再去带坏别人就好

#1


我原来也遇到过这样的问题,也没有解决哦

#2



xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"



                <data:DataGridTextColumn Binding="{Binding A}" Header="hello">
                    <data:DataGridTextColumn.HeaderStyle>
                        <Style TargetType="dataprimitives:DataGridColumnHeader">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <CheckBox Content="{Binding B}" IsChecked="True" />
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </data:DataGridTextColumn.HeaderStyle>
                </data:DataGridTextColumn>


试试这个

#3


没人帮我吗?  我问的东西很长见吧.

#4


XAML

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid Name="dg" AutoGenerateColumns="False" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" ItemsSource="{Binding Collection}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridCheckBoxColumn Binding="{Binding IsChecked,Mode=TwoWay}" >
                    <sdk:DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="sdk:DataGridColumnHeader">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <CheckBox Content="全选" IsChecked="{Binding IsAllSelected,Mode=TwoWay}" />
                                            <CheckBox Content="全不选" IsChecked="{Binding IsNoneSelected,Mode=TwoWay}" />
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </sdk:DataGridCheckBoxColumn.HeaderStyle>
                </sdk:DataGridCheckBoxColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>

每一项的model

public class TestModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }
    }

datagrid的model

public class TestModelForDataGrid : INotifyPropertyChanged
    {
        public TestModelForDataGrid()
        {
            Collection = new ObservableCollection<TestModel>();
            IsAllSelected = false;
            IsNoneSelected = true;
            for (int i = 0; i < 10; i++)
                Collection.Add(new TestModel { IsChecked = false });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private bool isAllSelected;

        public bool IsAllSelected
        {
            get { return isAllSelected; }
            set 
            {
                if (isAllSelected != value)
                {
                    isAllSelected = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsAllSelected"));
                    if (value)
                    {
                        IsNoneSelected = false;
                        foreach (var c in Collection)
                            c.IsChecked = true;
                    }
                }
            }
        }

        private bool isNoneSelected;

        public bool IsNoneSelected
        {
            get { return isNoneSelected; }
            set
            {
                if (isNoneSelected != value)
                {
                    isNoneSelected = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsNoneSelected"));
                    if (value)
                    {
                        IsAllSelected = false;
                        foreach (var c in Collection)
                            c.IsChecked = false;
                    }
                }
            }
        }

        public ObservableCollection<TestModel> Collection { get; set; }
    }

界面初始化

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            TestModelForDataGrid model = new TestModelForDataGrid();
            dg.DataContext = model;
        }
    }

如果LZ还是坚持“我就是不想要model”,那我也没办法了,在SL/WPF中不用这种方式,慢慢受折磨吧,这样只是实现列头的全选全不选,点击下面每个checkbox改变列头的选择,还需要在testmodel中ischecked属性set函数中操作父model,这个可以通过在子model中加个父model的引用的方式实现

#5


网上有不少实例,楼主可以参考:

http://silverlightchina.net/html/tips/2009/1118/241.html

http://silverlightchina.net/html/developer/silverlight/2010/1012/2582.html

#6


 我已经实现了.  没用model  谢谢大家了

#7


希望这人的“执着”不要误导大家,本来好端端的隔离,就被这么弄混了,SL/WPF提供给我们绑定这座桥,难道是用来看的吗

#8


你把代码贴上来看看的

#9


我这人就这样,  不喜欢按照别人的东西走。 我没发现放一个model的好处, 也许你体会到了. MVVM 我也没体会出好处. 所以我都给拿掉了. 我想说的是,使用要用灵魂不要光用壳子. 如果硬套MVVM  什么的。 没有减轻工作反而增加了。 你没有必要说我写的如何, 我也不会去问你采用的什么. 我现在就是按照我的方式走. 实践出效果. 而走到现在,  我省去了100多个表的 model维护 .  这是我看见确实的好处

#10


MVVM是理念,更是灵魂,如果一个SL/WPF的开发还抱着从前那种观念作者ASP.Net/Winform时代相同的事情,那么还不如不要进入新的时代,就在那个原始的时代一直待着,这根本不是什么硬套,如果一意孤行,后面的苦涩请慢慢体会,不过不要再去带坏别人就好