我需要帮助将Dictionary绑定到wpf DataGrid列:
什么工作?
使用int作为键(Dictionary<int, Object>
)的词典使用以下绑定路径
dgtc1.Binding = new Binding("ResourceDic1[0].Name");
什么不起作用?
使用int作为键(Dictionary<String, Object>
)的词典不能使用以下绑定路径,我需要帮助才能使此绑定工作:
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>
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();
}
}
}
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>();
}
}
}
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");
答案 0 :(得分:2)
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));
}
您正在添加:
Name_1
中不存在 ResourceDic2
密钥。
答案 1 :(得分:1)
从我的评论中:尝试js file