如何将基于键的字典值绑定到Code-Behind中的TextBlock? - c#

我将MVVM模型与动态字段生成器一起使用,该字段是通过数据库从字段中提取出来的,因为不同类型的表单需要不同的字段(TextBox / TextBlock,ComboBox等)。问题是我试图从字典中检索一个值,以显示在表单的TextBlock中,但是我不确定如何绑定检索到的键,以便显示该值。

目前,我正在执行以下操作:

 TextBlock textBlock = new TextBlock();
 textBlock.SetBinding(TextBlock.TextProperty, createFieldBinding(myPropertyName);

使用以下绑定方法:

 private Binding createFieldBinding(string fieldName) {
      Binding binding = new Binding(fieldName);
      binding.Source = this.DataContext;
      binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
      return binding;
 }

我在哪里传递类似Score的内容,它映射到ViewModel中的Score属性,但是如何绑定到Dictionary Key来检索其值?

我希望能够绑定到myDictionaryProperty[myDictionaryKey]之类的东西(如果可能)。

例:
下面为ID为1的Player生成PlayerScore
其中PlayerScoreDictionary<int, int>PlayerIDint

 <TextBlock Name="textBlockA" Text="{Binding PlayerScore[1]} />

参考方案

可以绑定到索引属性,并且使用与C#相同的符号,就像您写的一样:

<TextBlock Name="textBlockA" Text="{Binding PlayerScore[1]}" />

传递给“ createFieldBinding”的字符串是属性路径。如果将源设置为字典,则只需传递索引器部分,例如“ [1]”,就好像您已经在xaml中这样做一样:

<TextBlock Name="textBlockA" Text="{Binding [1]}" />

See this

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

如何在Wiremock中为JUNIT匹配精确的json - java

我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares�…

在datagrid(wpf)中选择行索引 - c#

如何在datagrid中选择行索引? 事件SelectionChanged以下代码不起作用: private DataGridRow dgr = new DataGridRow(); private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { thi…

jQuery DataTable TableTool在IE和Firefox中不起作用 - c#

我在MVC4 ASP.NET Web应用程序中使用Jquery DataTable TableTool。导出到Excel和PDF可以与Chrome完美配合。但是不能在IE和FireFox中使用。我的代码如下 dom: 'T<"clear">lfrtip', tableTools: { "sSwfP…