C# 自定义类的PropertyChanged始终为null
我对INotifyPropertyChanged和binding有一个问题,我在这里看了十几个问题,但问题仍然存在 我的XAML代码:C# 自定义类的PropertyChanged始终为null,c#,wpf,data-binding,C#,Wpf,Data Binding,我对INotifyPropertyChanged和binding有一个问题,我在这里看了十几个问题,但问题仍然存在 我的XAML代码: <Window x:Class="DAA.IST.ChoosePC.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window x:Class="DAA.IST.ChoosePC.MainWindow"
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:DAA.IST.ChoosePC"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Question x:Key="ExpertSystem" Text="question"/>
</Window.Resources>
<Grid DataContext="{StaticResource ExpertSystem}">
<Label x:Name="Description" Content="Определение опимальной конфигурации ПК" HorizontalAlignment="Left" Margin="100,30,0,0" VerticalAlignment="Top"/>
<Button x:Name="button" Content="" HorizontalAlignment="Left" Margin="204,167,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<Button x:Name="button1" Content="" HorizontalAlignment="Left" Margin="284,167,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
<Label x:Name="QuestionLable" Content="{Binding Text}" HorizontalAlignment="Left" Margin="100,70,0,0" VerticalAlignment="Top"/>
<Label x:Name="Result" Content="" HorizontalAlignment="Left" Margin="100,210,0,0" VerticalAlignment="Top"/>
</Grid>
请帮帮我,我肯定错过了什么或者不知道什么。另外,如果有人能给我关于更改IList答案的建议,我将感谢他^^ ^首先,在你的问题类中引用MainWindow是不好的做法。 您应该在此上下文中使用命令,即您应该在DataContext(此处的问题)中定义一个命令,并将该命令绑定到两个按钮的command属性 例如:
Class Question : INotifyPropertyChanged, IQuestion
{
public ICommand ButtonClickCommand { get; set;}
public Question()
{
//Bind ButtonClickCommand to Event Handler/ A Method
ButtonClickCommand = new DelegateCommand(EventHandlerName);
}
public void EventHandlerName()
{
}
}
另外,在类中获取一个私有的_answers字段,并从answers属性的Setter中引发OnPropertyChanged事件,以在通知机制中使用此列表,即
public IList<IAnswer> Answers {
get { return _answers }
set { _nswers = value;
OnPropertyChanged("Answers");
公共IList答案{
获取{return\u answers}
设置{u nswers=值;
关于财产变更(“答复”);
}
而且,您提到要更改标签和按钮的内容。但是要更改标签和按钮的内容,您还需要将这些控件的“内容”绑定到问题类中的属性。到目前为止,我在XAML中没有看到任何绑定。最后,我发现了我的错误。我应该更改属性,而不是创建新插件如果我创建一个新的,旧的值保持不变。它以某种方式与引用类型连接
MainWindow.Question.Text = "question";
MainWindow.Question.Answers[0].Text = "answer0";
MainWindow.Question.Answers[0].NextQuestionNumber = number0;
MainWindow.Question.Answers[1].Text = "answer1";
MainWindow.Question.Answers[1].NextQuestionNumber = number1;
你已经添加了所有的代码,但你的问题还不清楚。你到底面临什么问题?我需要更改
问题
类中标签和两个按钮的内容。据我所知,最好的方法是绑定和INotifyPropertyChanged
,但属性更改
始终为空,因为我还没有绑定按钮,但bel已绑定。您没有在答案的setter中提升PropertyChanged
。您需要在答案的setter中提升PropertyChanged
。很难确定问题是什么,但@RohitGarg可能回答了您的问题。
public IList<IAnswer> Answers {
get { return _answers }
set { _nswers = value;
OnPropertyChanged("Answers");
MainWindow.Question.Text = "question";
MainWindow.Question.Answers[0].Text = "answer0";
MainWindow.Question.Answers[0].NextQuestionNumber = number0;
MainWindow.Question.Answers[1].Text = "answer1";
MainWindow.Question.Answers[1].NextQuestionNumber = number1;