DataGrid 列绑定与字典 [英] DataGrid Column Binding with Dictionary

查看:30
本文介绍了DataGrid 列绑定与字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助将字典绑定到 wpf DataGrid 列:

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

什么是有效的?

以 int 为键的字典 (Dictionary) 使用以下绑定路径

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

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

什么不起作用?

以 int 为键的字典 (Dictionary) 无法使用以下绑定路径,我需要帮助才能使此绑定正常工作:

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");

这是重现问题的代码:

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>

代码 MainWindow.xaml.cs:

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();
        } 
    }
}

代码 Project.cs:

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>();
        }
    }
}

代码 PResource.cs:

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;
        }
    }
}

我需要如何构建绑定路径才能使其正常工作?

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

推荐答案

来自我的评论: try dgtc2.Binding = new Binding(@"ResourceDic2[Name_100].Name");

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

这篇关于DataGrid 列绑定与字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆