MVVM Light DataGrid绑定

时间:2015-05-25 09:05:42

标签: wpf mvvm data-binding datagrid mvvm-light

我正在使用C#,.NET Framework 4.5.1,MVVM Light框架和Entity Framework 6.1.3开发WPF。

我在窗口中有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属性都有)。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

尝试将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>