其他分享
首页 > 其他分享> > WPF 数据对象的绑定及转换

WPF 数据对象的绑定及转换

作者:互联网

一、WPF DataGrid数据的绑定

(1)列类型为DataGridCheckBoxColumn时,可绑定一个bool型变量,实现CheckBox是否选中
 <DataGridCheckBoxColumn Header="USE" Binding="{Binding IsUse}"/>
      private bool _isUse;
      public bool IsUse
      {
          get { return _isUse; }
          set
          {
              _isUse = value;
              RaiseProperChanged();
          }
      }
      
      public event PropertyChangedEventHandler PropertyChanged;
      private void RaiseProperChanged([CallerMemberName] string caller = "")
      {

          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(caller));
          }
      }

(2)列类型为DataGridComboBoxColumn时,可绑定列表,实现下拉列表的选择
 <DataGridComboBoxColumn Header="Parameters"  ItemsSource="{Binding Source={StaticResource ParasList}}" />
 <UserControl.Resources>
        <ObjectDataProvider x:Key = "ParasList" MethodName = "GetParasList" ObjectType = "{x:Type ClassName}">
         <!--ObjectType 指向实现方法的类名-->
        </ObjectDataProvider>
</UserControl.Resources>
        public List<string> GetParasList()
        {
            List<string> result = new List<string>();
            result.Add("=");
            result.Add(">");
            result.Add("<");
            result.Add("≥");
            result.Add("≤"); 
            return result;
        }

二、数据列表的序列化与反序列化

      (1)序列化保存
      public static void XmlSerialize<T>(T obj,string path)
      {
          try
          {
              XmlSerializer serializer = new XmlSerializer(obj.GetType());
              TextWriter tw = new StreamWriter(path);
              serializer.Serialize(tw, obj);
              tw.Close();
          }
          catch(Exception ex)
          {

          }
      }
(2)反序列化加载
      public static object XmlDserialize(Type t,string path)
      {
          try
          {
              using(FileStream fs = new FileStream(path,FileMode.Open))
              {
                  XmlSerializer serializer = new XmlSerializer(t);
                  return serializer.Deserialize(fs);
              }

          }
          catch (Exception ex)
          {
              return null;
          }
      }
测试
  //保存
  XmlSerializeTool.XmlSerialize(mAllData,path);
  //读取
  mAllData= XmlSerializeTool.XmlDserialize(mAllData.GetType(), path) as ObservableCollection<XXX>;

标签:转换,绑定,XmlSerializer,result,path,new,WPF,序列化,public
来源: https://blog.csdn.net/baidu_24565387/article/details/123140910