WPF中动态实体数据的属性失效的处理方法

WPF的datagrid 不能直接支持动态实体数据的属性设置,很让人郁闷。要知道这意思着,我们要写好多代码在前台去绑定,在后台去写事件。翻遍了网络也没找到好的解决方式,最后只好自己动手了,希望也能帮助到别人。

MainWindow.xaml:

 x:Class="DataGridDisplayNameTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:dg="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">
    
       x:Name="theGrid" AutoGenerateColumns="True" AutoGeneratingColumn="dg_AutoGeneratingColumn">
      
  

MainWindow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Reflection;

namespace DataGridDisplayNameTest {

 
  public partial class MainWindow : Window {
    public MainWindow() {
      this.Initialized += new EventHandler(MainWindow_Initialized);
      InitializeComponent();
    }

    /// 
    /// 添加数据
    /// 
    ///  name="sender">
    ///  name="e">
    void MainWindow_Initialized(object sender, EventArgs e) {
      this.theGrid.ItemsSource = new ObservableCollection<TestItem>{
        new TestItem {
          Value1 = 11, Value2 = 12, Value3 = 13
        },
        new TestItem {
          Value1 = 21, Value2 = 22, Value3 = 23
        },
        new TestItem {
          Value1 = 31, Value2 = 32, Value3 = 33
        }
      };
    }

    /// 
    /// Event handler for when columns are added to the data grid
    /// 
    ///  name="sender">
    ///  name="e">
    void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {

      string displayName = GetPropertyDisplayName(e.PropertyDescriptor,e.Column);
     

    }


    public static void GetPropertyDisplayName(object descriptor,System.Windows.Controls.DataGridColumn dataColumn) {

     PropertyDescriptor pd = descriptor as PropertyDescriptor;
     if (pd != null)
     {
       // 检查名称属性
       DisplayAttribute displayName = pd.Attributes[typeof(DisplayAttribute)] as DisplayAttribute;
       // 检查是否可编辑
       EditableAttribute editorAttribute = pd.Attributes[typeof(EditableAttribute)] as EditableAttribute;


       if (displayName != null)
       {
         dataColumn.Header = displayName.Name;  
       }


       if (editorAttribute != null)
       {
         dataColumn.IsReadOnly = !editorAttribute.AllowEdit;
       }
     }

  }

  public class TestItem {
    [DisplayName("Display Name 1")]
    public int Value1 { get; set; }
    [DisplayName("Display Name 2")]
    public int Value2 { get; set; }
    [DisplayName("Display Name 3")]
    public int Value3 { get; set; }

  }
}



你可能感兴趣的:(WPF中动态实体数据的属性失效的处理方法)