Click here to Skip to main content
15,881,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display multiple DataTables with fixed layout(columns are fixed and same for each table). I am using ItemsControls where ItemTemplate is Grid, and my collection of tables is in Dictionary.
My dictionary is dynamic and can change, and number of rows for each table is also dynamic and can change.
So I want to bind ItemsControl to my dictionary, where each Item is Binded to DataTable, and want to display that DataTable.
But I am not able to display any table.

What I have tried:

XAML
<ItemsControl ItemsSource="{Binding allGroup}">
    <ItemsControl.ItemTemplate>
       <DataTemplate>
            <DataGrid ItemsSource="{Binding Value.DefaultView, Mode=OneWay}" />
       </DataTemplate>
    </ItemsControl.ItemTemplate>


C#
public partial class MainWindow : Window
{
    Dictionary<string, DataTable> allGroup = new Dictionary<string, DataTable>();
    public MainWindow()
    {
        InitailizeComponent;
    }
}


I have added some tables into dictionary(allGroup) but can't see anything when running application.
Posted
Updated 7-Jun-21 5:54am
Comments
[no name] 5-Jun-21 19:25pm    
Easier to model a parent-child relation. The "keys" (strings) are the parent (List view). The children are the associated DataTable's DataRows (DataGrid). When the parent selection changes, you load the associated (child) DataTable.
Aditya Jain 2021 6-Jun-21 6:31am    
How to do that? Any source where I can see a code example of what you are saying.
[no name] 6-Jun-21 12:11pm    
Add a ListView and a DataGrid to a Window / Page. Load a ListView with the dictionary keys. In the "SelectionChanged" event of the ListView use the item (i.e. the dictionary "key") to retrieve the corresponding DataTable and load it into the DataGrid. (There are no examples ... the rest of the world uses MVVM and only creates dull single-view user interfaces)
Aditya Jain 2021 7-Jun-21 9:41am    
Got it, Thanks a lot for the help!!

1 solution

In the code you've shown, allGroup is a private field. WPF binding only works with public properties.

If you check the output window in Visual Studio, you should see at least one binding error telling you that the property allGroup could not be resolved.

Change your dictionary to be a public property. You'll also need to set the DataContext somewhere in the tree at or above your ItemsControl, so that WPF knows which object to look at to find the property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900