2015-10-22 66 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>