【发布时间】:2017-04-23 11:52:35
【问题描述】:
我在项目中使用绑定到可观察集合的数据网格,但自动生成的列顺序存在问题。
在我向绑定类添加新属性之前一切正常,现在在第三列中生成 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 事件函数中的 order 没有什么特别之处,只是样式应用。
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>
以及生成的网格顺序:
我可以更改什么以使名称列再次显示在第一个位置?
感谢您的帮助。
【问题讨论】: