标题列数据网格
本文关键字:网格 数据网 数据 标题 | 更新日期: 2023-09-27 18:18:56
我想创建一个数据网格,标题如下
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
列表时,我是否有办法修改数据网格上的显示标题?
您可以将标题文本属性绑定到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>
或
<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>