简体   繁体   English

我需要使用带有Caliburn.Micro的WPF在DataGrid上显示对象字典的数据,每个对象都有自己的字典

[英]I need to display data from a dictionary of objects each with their own dictionary on a DataGrid using WPF with Caliburn.Micro

So I have a Dictionary<string, Language> which is a collection of languages each containing a BindableCollection<Translation> keyed by the language name. 因此,我有一个Dictionary<string, Language> ,它是语言的集合,每种语言都包含以语言名称为键的BindableCollection<Translation> A Translation is just a data model containing a key and a value. Translation只是一个包含键和值的数据模型。 It's basically a dictionary, but I needed to bind the keys and values to text box's. 它基本上是一本字典,但是我需要将键和值绑定到文本框。 All the languages have the same list of keys. 所有语言都具有相同的键列表。 I would like to be able to bind the ItemsSource to my Dictionary<string, Language> and have it output something like the following. 我希望能够将ItemsSource绑定到我的Dictionary<string, Language>并使其输出类似以下内容。

Key       | English    | Spanish          | ...
______________________________________________
username  | Username   |Nombre de usuario | ...

The only way I have found to be able to do this is to put all my data into a DataTable, but I need to be able to edit and add and remove columns and rows. 我发现能够执行此操作的唯一方法是将所有数据放入DataTable,但是我需要能够编辑,添加和删除列和行。 My code is becoming a mess trying to wire up custom event handling to keep my data and my DataGrid in sync and I need to figure out a better more manageable solution. 试图连接自定义事件处理以使我的数据和DataGrid保持同步,我的代码变得一团糟,我需要找出一个更好,更易管理的解决方案。 Thanks for taking the time to help. 感谢您抽出宝贵的时间来帮助您。

This does not look like something you can handle with a few line of code. 这看起来好像不需要几行代码就可以处理。 Anyhow I would: 无论如何,我会:

  1. Create a Model with your Dictionary<string, Language> 使用Dictionary<string, Language>创建模型
  2. Create a ViewModel with a List<TranslationsRow> 使用List<TranslationsRow>创建一个ViewModel

TranslationsRow would look like this: TranslationsRow看起来像这样:

class TranslationsRow
{
  public string Key;
  public string English;
  public string Spanish;
  public string German:
  // and so on...
}
  1. Data bind you DataGrid to List<TranslationsRow> 数据将您的DataGrid绑定到List<TranslationsRow>

You have to code by yourself the mappings between the Model ( Dictionary<string, Language> ) and the ViewModel ( List<TranslationsRow> ). 您必须自己编写模型( Dictionary<string, Language> )和ViewModel( List<TranslationsRow> )之间的映射关系。 (In SQL this would be a PIVOT / UNPIVOT operation). (在SQL中,这将是PIVOT / UNPIVOT操作)。

I think this is a perfect example of a ViewModel "converting" the Model data on behalf of the View. 我认为这是ViewModel代表View“转换” Model数据的完美示例。

If I'm understanding correctly you have a dictionary which you want to bind to a TextBox while using the ShellViewModel of Caliburn Micro. 如果我理解正确,那么您将拥有一本字典,该字典想在使用Caliburn Micro的ShellViewModel时绑定到TextBox。

What I had to do in order to get the binding was set them all up in the code behind of the ShellView. 为了进行绑定,我必须要做的就是将它们全部设置在ShellView后面的代码中。 So I have a text box on the Window name=tb1. 所以我在Window name = tb1上有一个文本框。 In the constructor I coded: 在构造函数中,我编写了代码:

InitializeComponent(); 
tb1.DataContext = ViewModels.ShellViewModel.myDictionary;

Then in the tb1 XAML I set the binding: 然后在tb1 XAML中设置绑定:

Text={Binding [myKey].myProperty, Mode=TwoWay}

I had to do this for each one. 我必须为每个人这样做。 Name it\\Code behind\\Bind it for each textbox. 为每个文本框命名\\在其后的代码\\绑定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM