Не обновляется содержимое по нажатию кнопки
При нажатии кнопки должен добавляться вопрос, но этого не происходит
Код окна:
namespace TestSuit
{
/// <summary>
/// Логика взаимодействия для Constructor.xaml
/// </summary>
public partial class Constructor : Window, INotifyPropertyChanged
{
private int countQuestions = 0;
public ObservableCollection<CQuestion> Questions = new();
public ObservableCollection<CAnswer> Answers { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<CQuestion> MyCollection { get; set; }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Constructor()
{
InitializeComponent();
DataContext = this;
Answers = new();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Answers.Add(new CAnswer("Ответ"));
Questions.Add(new CQuestion
{
Text = "Вопрос",
CAnswers = Answers
});
MyCollection = Questions;
OnPropertyChanged();
Answers.Clear();
countQuestions++;
}
private void AddAnswer_Click(object sender, RoutedEventArgs e)
{
}
}
public class CQuestion
{
public string Text { get; set; }
public ObservableCollection<CAnswer> CAnswers { get; set; }
}
public class CAnswer
{
public CAnswer(string Text)
{
this.Text = Text;
}
public string Text { get; set; }
public bool Selected { get; set; }
public bool IsTrue { get; set; }
}
}
Разметка окна:
<Window x:Class="TestSuit.Constructor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestSuit"
mc:Ignorable="d"
Title="Constructor" Height="450" Width="800">
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="205*"/>
<ColumnDefinition Width="600*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ItemsControl x:Name="ControlMain" ItemsSource="{Binding MyCollection}" Margin="5,5,5,5" Grid.ColumnSpan="2">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Text}" FontWeight="Bold"/>
<ItemsControl ItemsSource="{Binding CAnswers}" Margin="5">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="{Binding DataContext.Text, RelativeSource={RelativeSource AncestorType=ItemsControl, AncestorLevel=1}}"
Content="{Binding Text}"
IsChecked="{Binding Selected}"
Margin="5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<Button x:Name="AddAnswer" Content="Добавить ответ" Click="AddAnswer_Click"/>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button x:Name="AddQuestion" Content="Добавить вопрос" Grid.Row="1" Margin="5,5,5,5" Click="Button_Click" Grid.ColumnSpan="2"/>
</Grid>
</ScrollViewer>
</Window>