您的位置:首页 > 编程语言 > C#

2013/08/23 C# WPF 学习笔记

2013-08-23 22:15 357 查看

自定义  Clone 类    复制一个对象

class CloneObj<T> where T : new()
{
/// <summary>
/// 利用反射和泛型
/// </summary>
public static T Clone(T obj)
{
T cloneObj = new T();
//定义一个临时变量
string tempName = string.Empty;
// 获得此模型的公共属性
PropertyInfo[] propertys = cloneObj.GetType().GetProperties();

//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
//将属性名称赋值给临时变量
tempName = pi.Name;
//判断此属性是否有Setter 该属性不可写,直接跳出
if (!pi.CanWrite) continue;
cloneObj.GetType().GetProperty(tempName).SetValue(
cloneObj,
obj.GetType().GetProperty(tempName).GetValue(obj, null),
null);
}
return cloneObj;
}
}

动态数据集合类 ObservableCollection

命名空间为: namespace System.Collections.ObjectModel

       // 摘要:

        //     表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

        //

        // 类型参数:

        //   T:

        //     集合中的元素类型。

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

到【2013-08-23】为止的经验(实现 数据驱动UI控件 |  UI控件变化 数据随即变化):

    ①集合中元素的类型T  需要实现 INotifyPropertyChanged 接口

        1)需要重写    类成员变量的  set的方法

        2)需要写 接口的 实现方法

public class Test : INotifyPropertyChanged
{

private int iD;
public int ID
{
get { return this.iD; }
set { if (value != this.iD) { this.iD = value; NotifyPropertyChanged("ID"); } }
}

//接口 自带的事件
public event PropertyChangedEventHandler PropertyChanged;
//实现事件的方法
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

     ②XAML 需要 Binding 的时候 需要加上  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged

<CheckBox Name="ckb_list" IsChecked="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center"></CheckBox>


【针对 DataGrid  的  DataGridRow 】,动态控制 IsEnabled 属性

功能要求

1数据没删除的时候 行可以被选中;

2 数据删除后 行不可以被选中 

可以实现的原因如下:

实现原理1:通过 RowStyle  可以对 IsEnabled 属性 进行 赋值

<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="True"></Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>


实现原理2:Value 值 可以 Binding

<Setter Property="IsEnabled" Value="{Binding IsDel}"></Setter>

实现原理3:转换器 可以根据Binding的值的变化,可以给控件返回不一样的结果

①转换器类

namespace BaseInfo
{
class ConvertIsEnable : IValueConverter
{
//Convert函数表示从数据源到目标的值转换
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int IsDel = (int)value;

if (IsDel == 1)
{
return false;
}
else
{
return true;

8fd7
}
}
 //ConvertBack函数表示从目标到数据源的值转换 (还不太清楚如何触发)
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
②在XAML引入 转换器

1)在Window 标签中引入 资源

<Window x:Class="BaseInfo.Base_List"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convert="clr-namespace:BaseInfo">
2)定义 Resource

<Window.Resources>
<!-- 定义资源 -->
<convert:ConvertIsEnable x:Key="enable_convert"/>
</Window.Resources>


3)为 IsEnabled属性进行 Binding

<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="{Binding IsDel, Converter={StaticResource  enable_convert}}"></Setter>
</Style>
</DataGrid.RowStyle>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: