How to bind WPF DataGrid to ObservableCollection

前端 未结 4 1100
情歌与酒
情歌与酒 2020-12-16 23:05

Can you give me a tip how to bind a WPF DataGrid to ObservableCollection. I had seen some posts and didn\'t find a direct answer. There and everywhere intricate problems are

相关标签:
4条回答
  • 2020-12-16 23:49

    You can have your dynamic data grid fill like this:

    ObservableCollection<CaseItem> data = new ObservableCollection<CaseItem>();
    this.CasesDataGrid.ItemsSource = data;
    

    But don't forget to bind the columns with the each item of your class.

    XAML code would be something like this:

    <DataGrid x:Name="CasesDataGrid" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Extended" CanUserAddRows="False" GridLinesVisibility="Horizontal">
    <DataGrid.Columns>
    <DataGridTextColumn Width="*" Header="ID" Binding="{Binding CaseID}"/>
    <DataGridTextColumn Width="*" Header="Date" Binding="{Binding CaseDate}"/>
    <DataGridTextColumn Width="*" Header="Plate" Binding="{Binding CasePlate}"/>
    <DataGridTextColumn Width="*" Header="Candidate" Binding="{Binding CaseCandidate}"/>
    <DataGridTextColumn Width="*" Header="Base" Binding="{Binding CaseBase}"/>
    <DataGridTextColumn Width="*" Header="Speed" Binding="{Binding CaseSpeed}"/>
    <DataGridTextColumn Width="*" Header="Photo" Binding="{Binding CasePhoto}"/>
    </DataGrid.Columns>
    </DataGrid>
    

    Hope it's useful to you.

    0 讨论(0)
  • 2020-12-16 23:50

    You should be able to do so by using the ItemsSource property of the grid and referencing your collection (probably located in your view model), like this:

    ItemsSource="{Binding Path=DownloadsCollection}" 
    

    Then add a binding on your columns to show the info (properties) of your MyClass objects in the collection.

    For a more detailed tutorial on how to do it, check this link.

    Edit:

    You can simply try something like this to see if everything works right and then move to custom columns:

    <DataGrid ItemsSource="{Binding DownloadsCollection}" />
    
    0 讨论(0)
  • 2020-12-16 23:56
    <DataGrid x:Name="employeeGrid" HorizontalAlignment="Center" VerticalAlignment="Center" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Emp #" Binding="{Binding EmpId}"/>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
        </DataGrid.Columns>
    </DataGrid>
    

    Following goes in your corresponding .cs file:

    employeeGrid.ItemsSource = employeeDetails;
    
    0 讨论(0)
  • 2020-12-16 23:59

    Datagrid binding in WPF check this answer. Basically you need to add ItemSource binding so your grid knows the datacontext.

    Than you need to add binding for datagrid columns, so it knows what to display. Hope this helped.

    Furthermore, you may want to add setter for your DownloadsCollection and binding mode if necessary. It is helpful if you need some updates.

    0 讨论(0)
提交回复
热议问题