简体   繁体   中英

Binding DataTable to DataGrid. WPF MVVM

I am trying to bind a Datatable on a Datagrid to be able to fill it dynamically. The Datagrid seems to find the Datatable because when I fill it and after the RaisePropertyChanged I have a lot of blank rows. There are no columns too.

My View:

<UserControl x:Class="NWViewer.View.DataGridView"
         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" 
         xmlns:local="clr-namespace:NWViewer.View"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         DataContext="{Binding DataGrid, Source={StaticResource Locator}}">
<Grid>
    <DataGrid ItemsSource="{Binding oTable.DefaultView}" AutoGenerateColumns="True" ColumnWidth="25">
    </DataGrid>
</Grid>
</UserControl>

My ViewModel:

public DataTable oTable { get;set;}

private void getNewData(List<ElementBaseViewModel> rootElement)
{    
    oTable.Clear();
    foreach (var element in rootElement)
    {
        buildFromChildren(element);                      
    }
    RaisePropertyChanged("oTable");                
}        
private void buildFromChildren(ElementBaseViewModel element)
    {
        if(element.Children != null)
        {
            if (isAttributeChildren(element))
            {
                DataRow oRow = oTable.NewRow();
                foreach (var attribute in element.AttributeChildren)
                {
                    Model.Attribute attr = (Model.Attribute)attribute.Element;
                    if (!oTable.Columns.Contains(attr.name))
                    oTable.Columns.Add(attr.name);
                    oRow[attr.name] = attr.Value;
                }
                oTable.Rows.Add(oRow);
            }
            foreach (var elem in element.ElementChildren)
            {
                buildFromChildren(elem);
            }
        }
    }

and this is the graphical rendering :

Datagrid

But the DataTable seems correctly filled when i debug it :

DataTable when debugging

The problem is most probably related to the DataTable initialization, DataGrid will auto-generate columns when a new ItemsSource is set, but it will not re-generate columns when columns are added to the underlying table after initialization.

Solution 1:

Create all columns on initialization of the DataTable , before binding it to the DataGrid .

Solution 2:

Force refresh the ItemsSource . It should work like this, but I highly recommend Solution 1 if possible:

var tempTable = oTable;
oTable = null;
RaisePropertyChanged("oTable");
oTable = tempTable;
RaisePropertyChanged("oTable");

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