Вопрос

Most posts regarding DataGrid AutoGenerateColumns seem to deal with how to circumvent default behavior. Unfortunately the default is what I am struggling with.

I have several database tables created with Entity Framework 6.0, and would like to display them in my View using one and the same Datagrid for all tables, and AutoGenerating the Columns. Desired outcome: for the class-specific Viewmodel bound at run-time, display the columns with headers and at least one row.

XAML of the View's user control:

<UserControl.Resources>
    <DataTemplate x:Key="TableDataTemplate">
        <DataGrid
            AutoGenerateColumns="True"
            GridLinesVisibility="All"
            HorizontalScrollBarVisibility="Visible"
            VerticalScrollBarVisibility="Visible">
        </DataGrid>
    </DataTemplate>
</UserControl.Resources>

<StackPanel Height="720" Width="980">
    <!-- shows user which menu item was chosen -->
    <TextBlock Text="{Binding DisplayName}"/>

    <!-- defines the Input data grid for adding to DB table -->
    <Grid Height="80" MaxHeight="200">
        <ItemsControl
            ItemsSource="{Binding CurrentDataTable}" 
            ItemTemplate="{StaticResource TableDataTemplate}">
        </ItemsControl>
    </Grid>

The user control is bound to a class-specific 'Class'TableViewModel, implemented for all Database Tables. One example:

class ClientsAdminTableViewModel : TableViewModel
{
    // the Property the view binds to
    private IList<Client> currentDataTable;

    public ObservableCollection<Client> CurrentDataTable
    {
        get { return CollectionExtensions.ToObservableCollection<Client>(currentDataTable); }
        set { currentDataTable = value; OnPropertyChanged("CurrentDataTable")}
    }

    public ClientsAdminTableViewModel()
    {
        DisplayName = Strings.ClientAdminDisplayName;
        currentDataTable = context.Clients.ToList<Client>();
    }

When bound to {Binding CurrentDataTable} this code produces a UI, where I can see a grey body (probably the rows) surrounded by horizontal and vertical scrollbars, on top of which one long line (which must be the header line), but no columns.

Any help is appreciated.

Это было полезно?

Решение 2

This code now works (Thank you J. King!) The trick was to get rid of the data template, create the data grid right in the grid with the exact same binding statements.

    <StackPanel Height="720" Width="980">
        <!-- shows user which menu item was chosen -->
        <TextBlock Text="{Binding DisplayName}"/>

        <!-- defines the Input data grid for adding to DB table -->
        <Grid Height="200" MaxHeight="400">

            <DataGrid 
                ItemsSource="{Binding CurrentDataTableNew}" 
                AutoGenerateColumns="True"
                GridLinesVisibility="All" 
                HorizontalScrollBarVisibility="Visible" 
                VerticalScrollBarVisibility="Visible" >
            </DataGrid>
        </Grid>
    </userControl>

Другие советы

your datagrid needs to have its itemsource set

try adding

ItemsSource="{Binding CurrentDataTable}"

to the datagrid declaration. Also, do you get any binding errors in the output window?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top