DataGrid自动生成列的奇怪行为

时间:2016-12-08 09:35:34

标签: c# wpf datagrid

我在项目中使用绑定到可观察集合的数据网格,但是我遇到了自动生成列顺序的问题。

在将新属性添加到绑定类之前,一切正常,现在Name属性在第三列中生成...

这是班级:

[StructLayout(LayoutKind.Sequential)]
[XmlRoot("ConfData")]
public class XapDataVals : XapBaseClass, INotifyPropertyChanged
{
    [XmlAttribute]
    public string Name { get; set; } = "";

    [XmlAttribute]
    public float Min { get; set; } = 0;

    [XmlAttribute]
    public float Max { get; set; } = 0;

    [XmlAttribute]
    public float ALVal { get; set; } = 0;

    /* more properties */

    public event PropertyChangedEventHandler PropertyChanged;
    protected void PropertyChangedNotify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public XapDataVals()
    {

    }
}

Datagrid的AutoGeneratinColumn事件函数中的顺序没有什么特别之处,只有样式应用。

datagrid的XAML代码:

<DataGrid x:Name="DgDataTable" Grid.Row="0" Grid.ColumnSpan="2" Margin="10,10,10,0" FrozenColumnCount="1" AutoGenerateColumns="true" HeadersVisibility="All" RowHeaderWidth="20" GridLinesVisibility="Horizontal"
              Style="{StaticResource AzureDataGrid}"  LoadingRow="dgDataTable_LoadingRow" AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" 
              AutoGeneratingColumn="DgDataTable_AutoGeneratingColumn" CellStyle="{StaticResource CommonCell}" CurrentCellChanged="DgDataTable_CurrentCellChanged" PreviewMouseLeftButtonDown="DgDataTable_PreviewMouseLeftButtonDown" 
              Drop="DgDataTable_Drop" AllowDrop="True" IsManipulationEnabled="True" PreviewMouseRightButtonDown="DgDataTable_PreviewMouseRightButtonDown" CanUserSortColumns="False">
        <DataGrid.RowHeaderStyle>
            <Style TargetType="DataGridRowHeader">
                <Setter Property="FontSize" Value="10"/>
                <Setter Property="Background" Value="LightCyan"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </DataGrid.RowHeaderStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderBrush" Value="Blue" />
                        <Setter Property="BorderThickness" Value="1" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Blue" />
                </Style.Resources>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.RowValidationRules>
            <DataErrorValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
        </DataGrid.RowValidationRules>
    </DataGrid>

生成的网格顺序:

enter image description here

我可以更改什么才能使名称列再次显示在位置?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

请阅读DataGrid.AutoGeneratedColumns Event

<sdk:DataGrid x:Name="yourcolorfulDatagrid"
        AutoGenerateColumns="True"
        AutoGeneratingColumn="yourcolorfulDatagrid_AutoGeneratingColumn"/>

然后事件:

private void yourcolorfulDatagrid_AutoGeneratedColumns(object sender, EventArgs e)
{
        yourcolorfulDatagrid_.Columns.FirstOrDefault(x => x.Header.ToString() == "Name").DisplayIndex = 0;
}