数据绑定类的定义
说明:可以在类中输入propdp,按两下Tab键,自动带出依赖属性的代码,这是一个快捷键
public class FData2:DependencyObject //注意要继承DependencyObject
{
//定义三个依赖属性
public static readonly DependencyProperty FSelectedProperty;
public static readonly DependencyProperty FItem2Property;
public static readonly DependencyProperty FItemProperty;
static FData2()
{
//在构造函数中注册三个依赖属性
//DependencyProperty.Register 参数说明,1:FSelected,暴露给外面的属性,2:typeof(bool),属性的类型
// 3:typeof(FData2),填写类名,4:new PropertyMetadata(false) 里面的false为属性初始化时的默认值
FSelectedProperty =
DependencyProperty.Register("FSelected", typeof(bool), typeof(FData2), new PropertyMetadata(false));
FItemProperty =
DependencyProperty.Register("FItem", typeof(string), typeof(FData2), new PropertyMetadata(""));
FItem2Property =
DependencyProperty.Register("FItem2", typeof(string), typeof(FData2), new PropertyMetadata(""));
}
public bool FSelected
{
get { return (bool)GetValue(FSelectedProperty); }
set { SetValue(FSelectedProperty, value); }
}
public string FItem
{
get { return (string)GetValue(FItemProperty); }
set { SetValue(FItemProperty, value); }
}
public string FItem2
{
get { return (string)GetValue(FItem2Property); }
set { SetValue(FItem2Property, value); }
}
}
====================================================================================
List<FData2> lst = null;
public RadGridView用数据绑定方式实现复选框全选全清()
{
InitializeComponent();
lst = new List<FData2>(){
new FData2{FSelected=false,FItem="内容1",FItem2="内容1-2"},
new FData2{FSelected=false,FItem="内容2",FItem2="内容2-2"},
new FData2{FSelected=false,FItem="内容3",FItem2="内容3-2"},
new FData2{FSelected=false,FItem="内容4",FItem2="内容4-2"},
new FData2{FSelected=false,FItem="内容5",FItem2="内容5-2"},
new FData2{FSelected=false,FItem="内容6",FItem2="内容6-2"}};
dgList.ItemsSource = lst;
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
SelCheckBox(true);
}
private void btnClearAll_Click(object sender, RoutedEventArgs e)
{
SelCheckBox(false);
}
private void SelCheckBox(bool bSel)
{
for (int i = 0; i < lst.Count; i++)
{
lst[i].FSelected = bSel; //由于定义了依赖属性,所以这里数据一改变,界面上的复选框也跟着改变,文本也一样
if (bSel)
{
lst[i].FItem = "选择";
}
else
{
lst[i].FItem = "非选择";
}
}
}
====================================================================================
//复选框选择的的一个转换类
public class SelDataConvert : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bSel = (bool)value;
return bSel;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bSel = (bool)value;
return bSel;
}
}
====================================================================================
关键代码
<navigation:Page.Resources>
<local:SelDataConvert x:Key="SelCTS" />
</navigation:Page.Resources>
定义转换器资源
<CheckBox IsChecked="{Binding FSelected, Mode=TwoWay,Converter={StaticResource SelCTS}}"/>
使用双向绑定,使用SelCTS转换器
xaml代码
<navigation:Page x:Class="SilverlightRadGridView复选框.RadGridView用数据绑定方式实现复选框全选全清"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="RadGridView用数据绑定方式实现复选框全选全清 Page" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:local="clr-namespace:SilverlightRadGridView复选框" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<navigation:Page.Resources>
<local:SelDataConvert x:Key="SelCTS" />
</navigation:Page.Resources>
<Grid x:Name="LayoutRoot" Width="600" Height="460" VerticalAlignment="Center" HorizontalAlignment="Center">
<telerik:RadGridView Height="285" HorizontalAlignment="Left" Margin="36,50,0,0" Name="dgList" VerticalAlignment="Top" Width="552" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" AutoGenerateColumns="False" >
<telerik:RadGridView.Columns>
<telerik:GridViewColumn Header="复选" Width="100" HeaderTextAlignment="Center">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding FSelected, Mode=TwoWay,Converter={StaticResource SelCTS}}"/>
</StackPanel>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
<telerik:GridViewColumn Header="内容1" Width="100" HeaderTextAlignment="Center">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding FItem,Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
<telerik:GridViewColumn Header="内容2" Width="100" HeaderTextAlignment="Center">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding FItem2,Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
<Button Content="全选" Height="31" HorizontalAlignment="Left" Margin="38,350,0,0" Name="btnSelectAll" VerticalAlignment="Top" Width="76" Click="btnSelectAll_Click" />
<Button Content="全清" Height="31" HorizontalAlignment="Left" Margin="120,350,0,0" Name="btnClearAll" VerticalAlignment="Top" Width="76" Click="btnClearAll_Click" />
<Button Content="获取行的值" Height="31" HorizontalAlignment="Left" Margin="494,350,0,0" Name="btnGetItem" VerticalAlignment="Top" Width="94" Click="btnGetItem_Click" />
<sdk:Label Height="22" HorizontalAlignment="Left" Margin="39,7,0,0" Name="label1" VerticalAlignment="Top" Width="128" Content="使用绑定方式" />
</Grid>
</navigation:Page>