How do I override ToString() for a Dictionary
-
19-09-2019 - |
Question
I have a Dictionary<string, FieldDefinition>
dependency property that when I bind it to a WPF list box I want it to just print the string (not the FieldDefinition).
Is there a way to do that?
Solution
I could be wrong, here, but I think you are looking for the Keys
property on the dictionary; this will return a collection of TKey
values (in your case, the 'string' part of your dictionary, not the FieldDefinition part, which incidentally would be available via the Values
Property)
<ListBox ItemsSource="{Binding MyDictionary.Keys}" />
OTHER TIPS
I would create a class that either implements IDictionary
public class CustomDictionary : IDictionary
{
...
}
or inherits Dictionary
public class CustomDictionary : Dictionary<string, FieldDefinition>
{
...
}
and override the ToString
method in this class like this:
public override string ToString()
{
return "My custom string";
}
Derive a class from Dictionary, override ToString().
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow