zoukankan      html  css  js  c++  java
  • WPF中数据源绑定数据控件的时候对数据源绑定数据做处理或者转换

    比如说数据源是dataTable,里面有个字段是加密过后的字段,那么绑定显示到数据源控件listview的时候肯定是要显示
    解密后的明文。
    如果麻烦的处理就是重构dataTable,把那个字段解密

    但是wpf有更简单方法,在绑定控件的时候可以调用一个方法来处理这个情况

    参考来源msdn  
    ms-help://MS.MSDNQTR.v90.chs/wpf_conceptual/html/b00aaa19-c6df-4c3b-a9fd-88a0b488df2b.htm

    //先定义一个转换类,继续IValueConverter接口,实现其中的两个方法,
    这两个是互逆的。第一个方法就是处理的方法,可以随便坐其他处理
    [ValueConversion(typeof(DateTime), typeof(String))]
    public class DateConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    DateTime date = (DateTime)value;
    return date.ToShortDateString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    string strValue = value.ToString();
    DateTime resultDateTime;
    if (DateTime.TryParse(strValue, out resultDateTime))
    {
    return resultDateTime;
    }
    return value;
    }
    }

    然后在xaml页面的资源中定义。src是程序集的引用名称
    <src:DateConverter x:Key="dateConverter"/>
    然后在绑定字段的时候用这个转换就可以
    <TextBlock Name="StartDateDTKey"
    Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}"
    Style="{StaticResource textStyleTextBlock}"/>



    本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

  • 相关阅读:
    Minimum Depth of Binary Tree leetcode java
    Maximum Depth of Binary Tree leetcode java
    Symmetric Tree leetcode java
    Same Tree leetcode java
    Binary Tree Postorder Traversal leetcode java
    Binary Tree Preorder Traversal leetcode java
    Binary Tree Inorder Traversal leetcode java
    Combinations leetcode java
    一键清除Centos iptables 防火墙所有规则
    阿里云centos7.7x64安装open,并配置ip转发和nat伪装
  • 原文地址:https://www.cnblogs.com/zjypp/p/2319325.html
Copyright © 2011-2022 走看看