数据绑定类的定义
说明:可以在类中输入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),new PropertyMetadata(""));
FItem2Property =
DependencyProperty.Register("FItem2",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"},FItem="内容3",FItem2="内容3-2"},FItem="内容4",FItem2="内容4-2"},FItem="内容5",FItem2="内容5-2"},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,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" 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,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" name="btnSelectAll" VerticalAlignment="top" WIDth="76" Click="btnSelectAll_Click" /> <button Content="全清" Height="31" HorizontalAlignment="left" margin="120,0" name="btnClearall" VerticalAlignment="top" WIDth="76" Click="btnClearall_Click" /> <button Content="获取行的值" Height="31" HorizontalAlignment="left" margin="494,0" name="btnGetItem" VerticalAlignment="top" WIDth="94" Click="btnGetItem_Click" /> <sdk:Label Height="22" HorizontalAlignment="left" margin="39,7,0" name="label1" VerticalAlignment="top" WIDth="128" Content="使用绑定方式" /> </GrID> </navigation:Page>
以上是内存溢出为你收集整理的Silverlight自定义依赖属性(实现RadGridView复选框全选)全部内容,希望文章能够帮你解决Silverlight自定义依赖属性(实现RadGridView复选框全选)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)