Abbas
Abbas

Reputation: 4070

DataGrid Column Binding with Dictionary

I need help in binding a Dictionary to a wpf DataGrid Column:

What is working?

A Dictionary with int as key (Dictionary<int, Object>) works with the following binding path

dgtc1.Binding = new Binding("ResourceDic1[0].Name");

What is not working?

A Dictionary with int as key (Dictionary<String, Object>) is not working with the following binding path and I need help in getting this binding working:

dgtc1.Binding = new Binding("ResourceDic1["Name_100"].Name");

Here is the code to reproduce the issue:

The XAML:

<Window x:Class="DataGridBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="3"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" Name="DataGrid1" AutoGenerateColumns="False"/>
        <GridSplitter Grid.Row="1" Height="3" Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
        <DataGrid Grid.Row="2" Name="DataGrid2" AutoGenerateColumns="False"/>
    </Grid>
</Window>

The Code MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace DataGridBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<Project> Projects = new List<Project>();

            for (int i = 0; i < 5; i++)
            {
                Project pj = new Project();
                for(int k = 0; k < 5; k++)
                {
                    pj.ResourceDic1.Add(k, new PResource(String.Format("Name_{0}", 100 + k), 100 + k, 0.25));
                    pj.ResourceDic2.Add(String.Format("Name_{0}", 100 + k), new PResource(String.Format("Name_{0}", 100 + k), 100 + k, 0.25));
                }
                Projects.Add(pj);
            }

            DataGrid1.ItemsSource = Projects;
            DataGridTextColumn dgtc1 = new DataGridTextColumn();
            dgtc1.Header = "Binding1";
            //////////////////////
            //This binding works//
            //////////////////////
            dgtc1.Binding = new Binding("ResourceDic1[0].Name");
            DataGrid1.Columns.Add(dgtc1);
            DataGrid1.Items.Refresh();

            DataGrid2.ItemsSource = Projects;
            DataGridTextColumn dgtc2 = new DataGridTextColumn();
            dgtc2.Header = "Binding2";
            /////////////////////////////
            //This binding doesn't work//
            /////////////////////////////
            dgtc2.Binding = new Binding(@"ResourceDic2[""Name_100""].Name");
            DataGrid2.Columns.Add(dgtc2);
            DataGrid2.Items.Refresh();
        } 
    }
}

The Code Project.cs:

using System;
using System.Collections.Generic;

namespace DataGridBinding
{
    public class Project
    {
        public Dictionary<int, PResource> ResourceDic1 { get; set; }
        public Dictionary<String, PResource> ResourceDic2 { get; set; }

        public Project()
        {
            ResourceDic1 = new Dictionary<int, PResource>();
            ResourceDic2 = new Dictionary<string, PResource>();
        }
    }
}

The Code PResource.cs:

using System;

namespace DataGridBinding
{
    public class PResource
    {
        public String Name { get; set; }
        public int Number { get; set; }
        public double Value { get; set; }

        public PResource(String Name, int Number, double Value)
        {
            this.Name = Name;
            this.Number = Number;
            this.Value = Value;
        }
    }
}

How do I need to structure the binding path so that it works?

dgtc2.Binding = new Binding(@"ResourceDic2[""Name_100""].Name");

Upvotes: 4

Views: 2470

Answers (2)

Sam
Sam

Reputation: 1424

From my comment: try dgtc2.Binding = new Binding(@"ResourceDic2[Name_100].Name");

Upvotes: 6

Jai
Jai

Reputation: 8363

for(int k = 0; k < 5; k++)
{
    pj.ResourceDic1.Add(k, new PResource(String.Format("Name_{0}", 100 + k), 100 + k, 0.25));
    pj.ResourceDic2.Add(String.Format("Name_{0}", 100 + k), new PResource(String.Format("Name_{0}", 100 + k), 100 + k, 0.25));
}

You are adding:

  • [0]: PResource object ||| "Name_100" : PResource object
  • [1]: PResource object ||| "Name_101" : PResource object
  • [2]: PResource object ||| "Name_102" : PResource object
  • [3]: PResource object ||| "Name_103" : PResource object
  • [4]: PResource object ||| "Name_104" : PResource object

Name_1 key does not exist in ResourceDic2.

Upvotes: 2

Related Questions