Search code examples
c#xamlmaui

MAUI codebehind binding a Entry Text to an dictionary value


I would like to bind the property Text of an Entry to a Value of a Dictionary in the Code Behind of my XAML. Currently this works only conditionally. If I specify the value in the dictionary, then this neatly displayed in the property text. However, if I change the value in the Entry, then it is not changed in the Dictionary.

I create a new entry during the runtime. The value is read from the dictionary and also displayed correctly. Unfortunately it works only in one direction. Writing a new value in the dictionary is not done.

var entry = new Entry {
    Placeholder = item.Key,
    ClassId = item.Key,
    Text = (String)keyValuePairs.Where(k => k.Key == item.Key).First().Value
};

In this case: the keyValuePairs is a Observable Property in the viewModel


Solution

  • Yes, path syntax is tricky and weird. But this should work.

    entry.SetBinding(Entry.TextProperty, $"{nameof(_viewModel.ResultsKeyValues)}[{item.Key}]");