media media - 11 days ago 5
C# Question

ItemControl does not work

I have the following

xaml
code:

<Grid>
<ScrollViewer BorderThickness="1" Margin="5">
<StackPanel>
<Button HorizontalAlignment="Center" Content="Show History"
Margin="10" Name="btnShowHistory" Click="btnShowHistory_Click" />
<ItemsControl Name="itemControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="2">
<telerik:RadExpander Header="{Binding Header}"
Content="{Binding Content}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</Grid>


And the following code behind:

private void btnShowHistory_Click(object sender, RoutedEventArgs e)
{
List<HistoryClass> res = App.bl.GetHistory();


foreach (var item in res)
{
StackPanel oStackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
oStackPanel.Children.Add(new TextBlock()
{
Foreground = Brushes.DarkBlue,
Text = item.word.ToString(),
Margin = new Thickness(10)
});
StackPanel oContent = new StackPanel();
foreach (var innerItem in item.meaning)
oContent.Children.Add(new TextBlock()
{
Foreground = Brushes.MediumPurple,
Text = innerItem.ToString(),
Margin = new Thickness(20, 10, 10, 10)
});

list.Add(new RadExpander() { Header = oStackPanel, Content = oContent });
}

this.itemControl.ItemsSource = list;
}


During the life cycle of my program
GetHistory()
changes the value of
res
. I do not know why the
ItemsControl
does not update. I am working on this more than usual and can't figure out the reason. Thanks for helps.

Answer

in the beginning of the C# function after the first instruction type the following code:
this.list = new List<RadExpander>();