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

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

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

1. xaml

  <sdk:DataGrid x:Name="UserDG"  AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn >
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="sdk:DataGridColumnHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridColumnHeader">
<Grid Height="" VerticalAlignment="Center">
<Border BorderBrush="#FF71A2F2" BorderThickness="" Height="" VerticalAlignment="Top" Margin="0,-1">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.477,0">
<GradientStop Color="#FFF4EEEE" Offset=""/>
<GradientStop Color="White" Offset=""/>
<GradientStop Color="#FF70C3E9" Offset="0.092"/>
<GradientStop Color="#FF77BFE0" Offset="0.933"/>
<GradientStop Color="#FFD0E3EB" Offset="0.486"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter.Content>
<CheckBox Margin="7,2" x:Name="cbSelectAll" Click="RefreshShow"></CheckBox>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> </sdk:DataGridTemplateColumn.HeaderStyle>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="CK" HorizontalAlignment="Center" VerticalAlignment="Center" Tag="{Binding LoginNM}" Click="ck_Checked" IsChecked="{Binding IsCheck,Mode=TwoWay}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="序号" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Padding="" HorizontalAlignment="Center"/>
</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>

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

  private bool isCheck;

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

3.xaml.cs后台代码如下

//添加行号
private void InitList()
{ UserList.LoadingRow += (o, e) =>
{
(UserList.Columns[].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex() + ).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

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

<ScrollViewer Grid.Row="" >
<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>
上一篇:【转】iOS 开发之协议protocal-代理传值delegate


下一篇:WPF下递归生成树形数据绑定到TreeView上