は非常に簡単です:辞書への結合、エントリーキーで辞書値にバインド自動的に不足しているエントリを作成
<TextBox Text="{Binding SomeDictionary[someKey]}" />
エントリー必須の存在しかし:
public Dictionary<string, string> SomeDictionary { get; } = new Dictionary<string, string>
{
{ "someKey", "someValue" },
};
そうでない場合(たとえば、空の辞書用)AN例外がスローされます:
System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'String') from 'SomeDictionary' (type 'Dictionary~2'). BindingExpression:Path=SomeDictionary[someKey]; DataItem='MainWindow' (Name=''); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
指定のデフォルト値を持つエントリを自動的に追加する方法はありますかdは例外の代わりにキーをバインドしますか?私は、再利用可能なXAMLソリューションを探しています
if (!SomeDictionary.ContainsKey("someKey"))
SomeDictionary.Add("someKey", "defaultValue");
textBox.SetBinding(TextBox.TextProperty, new Binding("[someKey]") { Source = SomeDictionary });
:結合は、コード内で行われている場合
は現在、これを実現することができます。
物事は私が考えています:
- 行動(添付プロパティ)、または上記の1つのようなコードを実行するためのマークアップ拡張機能を作成します。
- キャッチ何とか例外とまたは他に何を再バインド?
- 何かを行うために独自の
Binding
を作成しますが、正確に何?
おそらく、もっと簡単な方法はありますか?
変換を試しましたか?それは私がやることです。 –
とデフォルト値を作成するときに "somekey"値を維持しますか? –