2

動的フィールドジェネレーターで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;
 }

ScoreViewModelのプロパティにマップするのようなものを渡す場合Score、どのようにディクショナリキーにバインドしてその値を取得しますか?

myDictionaryProperty[myDictionaryKey]可能であれば、のようなものにバインドできるようにしたいと思います。

例:以下は、PlayerScoreIDが1のforPlayerを生成します。ここPlayerScoreでaDictionary<int, int>PlayerIDint。です。

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

2 に答える 2

3

インデックス付きプロパティへのバインドは可能であり、次のようにC#と同じ表記を使用します。

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

「createFieldBinding」に渡す文字列はプロパティパスです。ソースを辞書として設定する場合は、xamlで次のように行ったかのように、「[ 1 ]」のようなインデクサー部分を渡す必要があります。

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

これを見る

于 2012-12-10T15:47:02.473 に答える
3

@Clemensが提供するこのソリューションを使用して、辞書のデータ型に基づいて独自のDictionaryItemConverterを構築し、とをバインドするマルチバインドメソッドを作成することができましKeyDictionary

コンバータ:

 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 に答える