Home > Mobile >  Filling WPF DataGrid in C# with a Dictionary <String,MyClass>
Filling WPF DataGrid in C# with a Dictionary <String,MyClass>

Time:12-14

I want to fill datagrid with data from my dictionary. Binding column Binding="{Binding Key} works, but trying to bind value of MyClass (Binding="{Binding Value.myString}) i only get property not found on 'object'. What should I change? Thanks in advance.

CodePudding user response:

I can give you a sample.

Please see the following codes:

xaml:
Binding="{Binding MyclassDic[N_100].Id}"

cs:
xxx.Binding=new Binding(@"MyclassDic[N_100].Id");

MainWindow.xaml:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197" Width="514.5"
        Loaded="Window_Loaded">
    <Grid RenderTransformOrigin="0.5,0.5" Margin="0,0,-1,3">
        <Grid.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="0.338"/>
                <TranslateTransform/>
            </TransformGroup>
        </Grid.RenderTransform>
        <DataGrid Name="DataGrid" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Width="150" Binding="{Binding MyclassDic[N_100].Id}" />
                <DataGridTextColumn Header="Name" Width="150" Binding="{Binding MyclassDic[N_100].Name}" />
                <DataGridTextColumn Header="Age" Width="150" Binding="{Binding MyclassDic[N_100].Age}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs:

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

namespace WpfApp1 {
    public partial class MainWindow : Window {
        public MainWindow () {
            InitializeComponent();
            }

        private void Window_Loaded (object sender, RoutedEventArgs e) {
            List<Project> projects = new List<Project>();
            Project project = new Project();
            project.MyclassDic.Add("N_100", new Myclass(1, "Tim", 30));
            projects.Add(project);
            DataGrid.ItemsSource = projects;              
            }
        }
    }

Myclass.cs:

using System;
namespace WpfApp1 {
    public class Myclass
        {
        public int Id {
            get; set;
            }
        public String Name {
            get; set;
            }
        public int Age {
            get; set;
            }

        public Myclass (int Id,String Name, int Age) {
            this.Name = Name;
            this.Id = Id;
            this.Age = Age;
            }
        }
    }

Project.cs:

using System;
using System.Collections.Generic;

namespace WpfApp1 {
    public class Project {
        public Dictionary<String, Myclass> MyclassDic {
            get; set;
            }
        public Project () {
            MyclassDic = new Dictionary<String, Myclass>();
            }
        }
    }

Output:

enter image description here

  • Related