Silverlight 中DataGrid中全选与非全选问题

问题:当点击全选时,全选所有的复选框,但是滚动屏幕时,却复选框就会取消选中

一、解决方法(将要展示的实体数据模型添加bool属性,在数据绑定时添加click时间,盘带选中的状态,就可以了)

1. xaml

 1  <sdk:DataGrid x:Name="UserDG"  AutoGenerateColumns="False">
 2                 <sdk:DataGrid.Columns>
 3                     <sdk:DataGridTemplateColumn >
 4                             <sdk:DataGridTemplateColumn.HeaderStyle>
 5                                 <Style TargetType="sdk:DataGridColumnHeader">
 6                                     <Setter Property="Template">
 7                                         <Setter.Value>
 8                                             <ControlTemplate TargetType="sdk:DataGridColumnHeader">
 9                                                 <Grid Height="31" VerticalAlignment="Center">
10                                                     <Border BorderBrush="#FF71A2F2" BorderThickness="1" Height="32"  VerticalAlignment="Top" Margin="0,-1">
11                                                         <Border.Background>
12                                                             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.477,0">
13                                                                 <GradientStop Color="#FFF4EEEE" Offset="0"/>
14                                                                 <GradientStop Color="White" Offset="1"/>
15                                                                 <GradientStop Color="#FF70C3E9" Offset="0.092"/>
16                                                                 <GradientStop Color="#FF77BFE0" Offset="0.933"/>
17                                                                 <GradientStop Color="#FFD0E3EB" Offset="0.486"/>
18                                                             </LinearGradientBrush>
19                                                         </Border.Background>
20                                                     </Border>
21                                                     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
22                                                         <ContentPresenter.Content>
23                                                             <CheckBox Margin="7,2" x:Name="cbSelectAll" Click="RefreshShow"></CheckBox>
24                                                         </ContentPresenter.Content>
25                                                     </ContentPresenter>
26                                                 </Grid>
27                                             </ControlTemplate>
28                                         </Setter.Value>
29                                     </Setter>
30                                 </Style>
31 
32                             </sdk:DataGridTemplateColumn.HeaderStyle>
33                             <sdk:DataGridTemplateColumn.CellTemplate>
34                                 <DataTemplate>
35                                 <CheckBox x:Name="CK" HorizontalAlignment="Center"  VerticalAlignment="Center" Tag="{Binding LoginNM}"  Click="ck_Checked"   IsChecked="{Binding IsCheck,Mode=TwoWay}"/>
36                                 </DataTemplate>
37                             </sdk:DataGridTemplateColumn.CellTemplate>
38                         </sdk:DataGridTemplateColumn>
39                         <sdk:DataGridTemplateColumn Header="序号" >
40                             <sdk:DataGridTemplateColumn.CellTemplate>
41                                 <DataTemplate>
42                                     <TextBlock  Padding="5" HorizontalAlignment="Center"/>
43                                 </DataTemplate>
44                             </sdk:DataGridTemplateColumn.CellTemplate>
45                         </sdk:DataGridTemplateColumn>
46                     <sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
47                     <sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
48                     <sdk:DataGridTextColumn Header="Email" Binding="{Binding Email}"/>
49                 </sdk:DataGrid.Columns>
50             </sdk:DataGrid>
View Code

2. 在要展示的是实体数据模型里添加 bool类型的属性,并绑定到xaml页面中

  private bool isCheck;

        public bool IsCheck
        {
            get { return isCheck; }
            set { isCheck = value; NotifyPropertyChanged("IsCheck"); }
        }
View Code

3.xaml.cs后台代码如下

//添加行号
 private void InitList()
        {

            UserList.LoadingRow += (o, e) =>
            {
                (UserList.Columns[1].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex() + 1).ToString();
            };
        }
  #region 全选/不全选解决方法
        private void RefreshShow(object sender, System.Windows.RoutedEventArgs e)
        {
            _selectList.Clear();
            CheckBox quxuan = (CheckBox)sender;
            if (this.UserList.ItemsSource != null)
            {
                if (quxuan.IsChecked.Value)
                {
                    foreach (var item in userinfos)
                    {
                        item.IsCheck = true;
                        _selectList.Add(item.LoginNM);
                    }
                    #region
                    //foreach (var obj in this.UserList.ItemsSource)
                    //{
                    //    var col = this.UserList.Columns[0].GetCellContent(obj);
                    //    if (col != null)
                    //    {
                    //        //下面就是获取选中或取消的CheckBox
                    //        CheckBox cb1 = this.UserList.Columns[0].GetCellContent(obj).FindName("CK") as CheckBox;
                    //        var id = (string)cb1.Tag;
                    //        cb1.IsChecked = quxuan.IsChecked;


                    //    }
                    //}
                    #endregion
                }
                else
                {
                    foreach (var item in userinfos)
                    {
                        item.IsCheck = false;
                        _selectList.Remove(item.LoginNM);
                    }
                  
                }
            }
            else
            {
                MessageBox.Show("当前没有数据可选择!");
                quxuan.IsChecked = false;
                return;
            }

        }
        private List<string> _selectList = new List<string>();
        private void ck_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in userinfos)
            {
                if (item.IsCheck)
                {
                    if (!_selectList.Contains(item.LoginNM))
                    {
                        _selectList.Add(item.LoginNM);
                    }
                }
                else
                {
                    _selectList.Remove(item.LoginNM);
                }
            }
           
        }
        #endregion
View Code


二、解决方法(在datagrid数据展示外面添加滚动条<ScrollViewer Grid.Row="1" >,后台添加相应的方法,就可以了)

<ScrollViewer Grid.Row="1" >
            <sdk:DataGrid x:Name="UserDG"  AutoGenerateColumns="False">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn Header="Id">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="cb" Tag="{Binding Id}"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                    <sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
                    <sdk:DataGridTextColumn Header="Email" Binding="{Binding Email}"/>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </ScrollViewer>
View Code

 

 

posted @ 2014-04-21 13:51  银河系上的地球  阅读(300)  评论(0编辑  收藏  举报