2015-10-22 64 views
0

我想创建一个DataGrid与下面的头标题列的DataGrid

ID Name Key Unique Null Value Type 
---------------------------------------- 

现在我有两个类:dynamicentitynewproperty

dynamicentity如下

private int ID; 
    private string entityName; 
    private string Entitydescription; 
    private List<NewProperty> addedProperties; 

    public DynEntity(string name, string desc) 
    { 
     entityName = name; 
     Entitydescription = desc; 
     addedProperties = new List<NewProperty>(); 
    } 

    public bool addNP(NewProperty data) 
    { 
     addedProperties.Add(data); 
     return true; 
    } 

newproperty类是如下

private int ID; 
    private string PropertyName; 
    private string StringPropertie; 
    private bool isString; 
    private int IntPropertie; 
    private bool isInt; 
    private float floatPropertie; 
    private bool isFloat; 
    private DateTime DatetimePropertie; 
    private bool isDate; 
    private bool boolPropertie; 
    private bool isBool; 
    private bool isKey; 
    private bool allowNull; 
    private bool isFK; 
    private string FKEntityName; 
    private bool isUnique; 
    public NewProperty(int _id, string _propertyname, bool propType, bool key, bool isnull, bool isunic, string fkname) 
    { 
     ID = _id; 
     PropertyName = _propertyname; 
     boolPropertie = propType; 
     isInt = false; 
     isString = false; 
     isFloat = false; 
     isDate = false; 
     isBool = true; 
     isKey = key; 
     allowNull = isnull; 
     isUnique = isunic; 
     if (fkname == "") 
     { 
      isFK = false; 
     } 
     else 
     { 
      isFK = true; 
      FKEntityName = fkname; 
     } 
    } 

    [DisplayName("ID")] 
    public int PropID 
    { 
     get { return ID; } 
     set 
     { 
      ID = value; 
      OnPropertyChanged("ID"); 
     } 
    } 

    [DisplayName("Name")] 
    public string PropName 
    { 
     get { return PropertyName; } 
     set 
     { 
      PropertyName = value; 
      OnPropertyChanged("Name"); 
     } 
    } 

我的XAML是如下

<DataGrid Name="dgUsers" ItemsSource="{Binding DEPropID}" AutoGenerateColumns="True" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1">       </DataGrid> 

我的视图模型为:

private DynEntity _localDE; 

     public DynEntityViewModel(DynEntity DynamicEntity) 
     { 
      _localDE = DynamicEntity; 
     } 

     public string DEName 
     { 
      get 
      { 
       return _localDE.EntityName; 
      } 
      set 
      { 
       _localDE.EntityName = value; 
      } 
     } 

     public string DEDesc 
     { 
      get { return _localDE.EntityDescription; } 
      set 
      { 
       _localDE.EntityDescription = value; 
      } 
     } 

     public List<NewProperty> DEPropID 
     { 
      get { return _localDE.EntityProperties; } 
     } 

     public String Name 
     { 
      get { return _localDE.EntityProperties[0].PropName; } 
     } 

当返回列表newproperty有没有办法让我修改数据网格上的显示标题?

回答

1

您可以将标题文本属性绑定到一个ViewModel属性与文本标题动态改变,或干脆添加DataGridTextColumn你想:

是这样的:

<DataGrid Name="dgUsers" ItemsSource="{Binding DEPropID}" AutoGenerateColumns="False" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1"> 
     <DataGrid.Columns> 
     <DataGridTextColumn Header="YourText" Width="*" Binding="{Binding YourProperty}"/>  
     </DataGrid.Columns>     
</DataGrid> 

OR

<DataGrid Name="dgUsers" ItemsSource="{Binding DEPropID}" AutoGenerateColumns="False" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="{Binding DataContext.YourStringProperty}" Width="*" Binding="{Binding YourProperty}"/>      
    </DataGrid.Columns> 
</DataGrid>