cs
public void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
var propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
if (propertyDescriptor.IsBrowsable)
{
e.Column.Header = propertyDescriptor.DisplayName;
}
else
{
e.Cancel = true;
}
}
实体类中属性:
cs
[Browsable(true), DisplayName("ID"), ReadOnly(true)]
public int Id { get => _id; set => _id=value; }
前端:
XML
<DataGrid x:Name="dataGrid_Hexapod"
AutoGenerateColumns="True"
cm:Message.Attach="[Event AutoGeneratingColumn]=[Action OnAutoGeneratingColumn($eventArgs)]"
ContextMenu="{StaticResource ResourceKey=ContextMenu}"
hc:DataGridAttach.CanUnselectAllWithBlankArea="False"
HeadersVisibility="All"
ColumnWidth="150"
ItemsSource="{Binding SixAxisRobotPositions,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridStyle}"
SelectedIndex="{Binding SixAxisRobotSelectedIndex}">
</DataGrid>
想要绑定后端列表, AutoGenerateColumns="True"必不可少。