2

我正在使用带有动态字段生成器的 MVVM 模型,其中字段是从数据库中提取的,这样做是因为不同类型的表单需要不同的字段(TextBox/TextBlock、ComboBox 等)。问题是我正在尝试从字典中检索一个值,以在表单的 TextBlock 中显示,但我不确定如何绑定检索到的 Key 以便我可以显示该值。

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

 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,它映射到ScoreViewModel 中的一个属性,但是我如何绑定到字典键来检索它的值?

如果可能的话,我希望能够绑定到类似的东西myDictionaryProperty[myDictionaryKey]

示例:下面生成PlayerScoreID 为 1 的 for Player。其中PlayerScoreaDictionary<int, int>PlayerIDint

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

2 回答 2

3

使用@Clemens 提供的这个解决方案,我能够根据我的 Dictionary 的数据类型构建我自己的 DictionaryItemConverter,并创建一个将Keyand the绑定Dictionary在一起的多绑定方法。

转换器:

 public class DictionaryItemConverter : IMultiValueConverter {
      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
           if(values != null && values.Length >= 2) {
                var myDict = values[0] as IDictionary;
                if(values[1] is string) {
                     var myKey = values[1] as string;
                     if(myDict != null && myKey != null) {
                          //the automatic conversion from Uri to string doesn't work
                          //return myDict[myKey];
                          return myDict[myKey].ToString();
                     }
                }
                else {
                     long? myKey = values[1] as long?;
                     if(myDict != null && myKey != null) {
                          //the automatic conversion from Uri to string doesn't work
                          //return myDict[myKey];
                          return myDict[myKey].ToString();
                     }
                }
           }

           return Binding.DoNothing;
      }

      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
           throw new NotSupportedException();
      }
 }

多重绑定方法:

 private MultiBinding createFieldMultiBinding(string fieldName) {
      // Create the multi-binding
      MultiBinding mbBinding = new MultiBinding();
      // Create the dictionary binding
      Binding bDictionary = new Binding(fieldName + "List");
      bDictionary.Source = this.DataContext;
      // Create the key binding
      Binding bKey = new Binding(fieldName);
      bKey.Source = this.DataContext;
      // Set the multi-binding converter
      mbBinding.Converter = new DictionaryItemConverter();
      // Add the bindings to the multi-binding
      mbBinding.Bindings.Add(bDictionary);
      mbBinding.Bindings.Add(bKey);

      return mbBinding;
 }
于 2012-12-10T17:19:31.150 回答
3

可以绑定到索引属性,并且使用与 C# 相同的表示法,就像您写的那样:

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

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

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

看到这个

于 2012-12-10T15:47:02.473 回答