我想创建一个带有以下标题的数据网格
ID Name Key Unique Null Value Type
----------------------------------------
现在我有两个课程:dynamicentity
和newproperty
。
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
列表时,有没有办法修改数据网格上的显示标题?
答案 0 :(得分: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>