'UWP : Bind a Dictionary<string, List<ValidationResult>> to nested controls
C#, UWP
I have a Dictionary<string, List> object and i am trying to bind it to a nested control. but nothing is displayed on screen... I am not sure where i am wrong. here ValidationResult is from System.ComponentModel.DataAnnotations class
public readonly Dictionary<string, List<ValidationResult>> _errors = new Dictionary<string, List<ValidationResult>>();
public Dictionary<string, List<ValidationResult>> Errors
{
get { return _errors; }
}
Errors.Count does show me some value, but then ItemsControl binding does not produce any results.
<ItemsControl ItemsSource="{x:Bind VM.Errors , Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Text="->" />
<TextBlock Text="{Binding Key}" />
<ListBox ItemsSource="{Binding Value}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding ErrorMessage}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
what i am doing wrong, Please advice.
Regards
Solution 1:[1]
We can't find you initialize Error dictionary in your page's view model? it could work when I use fake data testing. Please refer the following to check your code.
public class ViewModel
{
public ViewModel()
{
var list = new List<ValidationResult>();
for (int i = 0; i < 10; i++)
{
list.Add(new ValidationResult($"error{i.ToString()}"));
}
Errors.Add("section1", list);
}
public readonly Dictionary<string, List<ValidationResult>> _errors = new Dictionary<string, List<ValidationResult>>();
public Dictionary<string, List<ValidationResult>> Errors
{
get { return _errors; }
}
}
Usage
public sealed partial class MainPage : Page
{
public ViewModel VM { get; set; }
public MainPage()
{
this.InitializeComponent();
VM = new ViewModel();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
