MVVM Light DataGrid 绑定

MVVM Light DataGrid binding

我正在使用 C#、.NET Framework 4.5.1、MVVM Light 框架和 Entity Framework 6.1.3.

开发 WPF

我在 window 中有这个 DataGrid:

<DataGrid x:Name="LevelConfigurationDataGrid" Grid.Column="1" Grid.Row="1" Margin="20,0" ItemsSource="{Binding LevelConfigs}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding CODE_LEVE}" Header="Nivel"/>
        <DataGridTextColumn Binding="{Binding CODE_NAME}" Header="Nombre"/>
        <DataGridTextColumn Binding="{Binding IS_ACTIVE}" Header="¿Activa?"/>
        <DataGridTextColumn Binding="{Binding CODE_TYPE}" Header="Tipo de código"/>
        <DataGridTextColumn Binding="{Binding CODE_SOURCE}" Header="Origen del código"/>
        <DataGridTextColumn Binding="{Binding HELPER_CODE_TYPE}" Header="Tipo de Helper Code"/>
        <DataGridTextColumn Binding="{Binding HELPER_CODE_SOURCE}" Header="Origen del helper code"/>
        <DataGridTextColumn Binding="{Binding QUANTITY}" Header="Cantidad"/>
        <DataGridTextColumn Binding="{Binding REMAINING_CODES}" Header="Restantes"/>
        <DataGridTextColumn Binding="{Binding TRZ_ENDPOINT}" Header="TRZ IP"/>
        <DataGridTextColumn Binding="{Binding TRZ_ENDPORT}" Header="TRZ Puerto"/>
    </DataGrid.Columns>
</DataGrid>

MainViewModel 上:

public ObservableCollection<CODE_LEVEL_CONFIGURATION> LevelConfigs
{
    get { return m_LevelConfigs; }
    set
    {
        m_LevelConfigs = value;
        RaisePropertyChanged("LevelConfigs");
    }
}

CODE_LEVEL_CONFIGURATION 上:

public class CODE_LEVEL_CONFIGURATION
{
    public byte CODE_LEVEL { get; set; }
    public string LEVEL_NAME { get; set; }
    public bool IS_ACTIVE { get; set; }
    public byte CODE_TYPE { get; set; }
    public byte CODE_SOURCE { get; set; }
    public byte? HELPER_CODE_TYPE { get; set; }
    public byte? HELPER_CODE_SOURCE { get; set; }
    public int QUANTITY { get; set; }
    public int REMAINING_CODES { get; set; }
    public string TRZ_ENDPOINT { get; set; }
    public int? TRZ_ENDPORT { get; set; }

    public virtual ICollection<CODES> Codes { get; set; }
    public virtual ICollection<HELPER_CODES> HelperCodes { get; set; }
}

但是,当我在 DataGrid 上加载数据时,我得到了我在 XAML 上定义的更多列。我得到了我的专栏和 13 个专栏(每个 CODE_LEVEL_CONFIGURATION 属性)。

我做错了什么?

尝试将 AutoGenerateColumns 设置为 False

<DataGrid x:Name="LevelConfigurationDataGrid"  AutoGenerateColumns="False" 
          Grid.Column="1" Grid.Row="1" Margin="20,0" ItemsSource="{Binding LevelConfigs}">
    <DataGrid.Columns>
        ...
    </DataGrid.Columns>
</DataGrid>