简体   繁体   中英

How to properly bind a list to a WPF DataGrid

I'm trying to bind a list to a datagrid using ItemSource:

xaml.cs

<UserControl x:Class="DDCUI.CommDiagnosisWPFCtrl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="800" Width="300">
    <DockPanel>
        <DataGrid DockPanel.Dock="Top" Height="300" AutoGenerateColumns="True" Name="DGComm" CanUserResizeColumns="True" IsReadOnly="True" ItemsSource="{Binding Source=dataGridRows}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="No." Binding="{Binding Number}" Width="0.1*"/>
                <DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="0.1*" />
                <DataGridTextColumn Header="Protocol" Binding="{Binding Protocol}" Width="0.15*" />
                <DataGridTextColumn Header="Source" Binding="{Binding Source}" Width="0.15*" />
                <DataGridTextColumn Header="Destination" Binding="{Binding Destination}" Width="0.15*" />
                <DataGridTextColumn Header="Data" Binding="{Binding Data}" Width="0.5*" />
            </DataGrid.Columns>
        </DataGrid>
        <RichTextBox DockPanel.Dock="Bottom" Height="150" Name="RtbHexCode"/>
        <TreeView DockPanel.Dock="Bottom" Height="200" Name="TreeViewDecode"/>

    </DockPanel>
</UserControl>

code:

public class CommDGDataSource
{
    public string Number { get; set; }
    public string Time { get; set; }
    public string Protocol { get; set; }
    public string Source { get; set; }
    public string Destination { get; set; }
    public string Data { get; set; }
}

private List<object> dataGridRows = new List<object>();

private void DGAddRow(string name, FunctionType ft)
{
    CommDGDataSource ds = new CommDGDataSource();
    ds.Protocol = name;
    ds.Source = "";
    ds.Destination = "";
    ds.Number = rowCount.ToString();
    ds.Data = "";
    ds.Time = "";
    dataGridRows.Add(ds);
}

When DGAddRow is called, the DataGrid doesn't update its values.

What am I doing wrong here? Any help would be appreciated.

Also, the XAML pre-generates empty rows even before they are filled with data. I want to be able to display only the rows that are filled with items (and add the new rows to the bottom of the last filled row). How can I accomplish this?

使用ObservableCollection <>而不是List <>,并使dataGridRows公开。

Ok, the first thing that surprises me is that dataGridRows is private field. If I'm not mistaken, you can't bind to private field and it's better to make it public property.(correct me if I'm wrong).

The second thing bothering me is that one AutoGenerateColumns="True" , not sure how grid works when AutoGenerateColumns is True and columns are explicitely specified. Try to set AutoGenerateColumns="False" .

And the third one: as @Rover has already said, try to set type datagridRows to ObservableCollection<CommGDDataSource>

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