简体   繁体   中英

WPF DataGrid data and columns not visible

What I have done is set up datagrid with multi row selection support. However the issue is that when AutoGenerateColumns is set to false the multi row selection works however the data that I upload from my data base does not show. When I switch AutoGenerateColumns to true, I can see the data but the single click multi select feature stops working.

Here is the xaml code:

<DataGrid x:Name="Hello" ItemsSource="{Binding Path=Albums}"
              Grid.Row="0"
              AutoGenerateColumns="True"  SelectionMode="Extended" Loaded="dgCustomers_Loaded">
                    <DataGrid.Columns>
                        <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    EditingElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    Header="IsSelected"
                                    Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}"
                                    />
                    </DataGrid.Columns>
                </DataGrid>

This is how I am filling in the datagrid:

private void dgCustomers_Loaded(object sender, RoutedEventArgs e)
    {

        var grid = sender as DataGrid; //Creates its self as an object
        grid.ItemsSource = CustomerDataAdapter.GetData(); //Shows all of the data in the Customer table
    }

I don't think you can do both auto generate columns & specify columns explicitly in the XAML. I believe it's one or the other. I believe you have to set AutoGenerateColumns to false & specify each column & a binding for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM