C# 文本块';在ViewModel中更改时,s文本值在视图中不更新
我用命令创建了一个提交按钮,根据用户是否在文本框中输入了正确的数字来更改文本块中的文本C# 文本块';在ViewModel中更改时,s文本值在视图中不更新,c#,wpf,data-binding,textblock,C#,Wpf,Data Binding,Textblock,我用命令创建了一个提交按钮,根据用户是否在文本框中输入了正确的数字来更改文本块中的文本 public string txtResults { get; set; } public string txtInput { get; set; } // Method to execute when submit command is processed public void submit() { if (txtInput == num
public string txtResults { get; set; }
public string txtInput { get; set; }
// Method to execute when submit command is processed
public void submit()
{
if (txtInput == number.ToString())
txtResults = "Correct!";
else
txtResults = "Wrong!";
}
“txtInput”是绑定到文本框并包含用户输入的成员txtResults'应该显示在文本块中。现在,当我在调试模式下单击submit按钮时,txtResults值被指定为“Correct!”字符串,但它不会在视图中更新
XAML:
<Window x:Class="WpfMVVP.WindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfMVVP"
Title="Window View" Height="350" Width="525" Background="White">
<Grid>
<Canvas>
<Label Canvas.Left="153" Canvas.Top="89" Content="Guess a number between 1 and 5" Height="28" Name="label1" />
<TextBox Text="{Binding txtInput, UpdateSourceTrigger=PropertyChanged}" Canvas.Left="168" Canvas.Top="142" Height="23" Name="textBox1" Width="38" />
<TextBlock Text="{Binding txtResults}" Canvas.Left="257" Canvas.Top="142" Height="23" Name="textBlock1" />
<Button Command="{Binding Submit}" Canvas.Left="209" Canvas.Top="197" Content="Submit" Height="23" Name="button1" Width="75" />
</Canvas>
</Grid>
现在它开始工作了!谢谢 请确保您的txtResults属性继承自INotifyPropertyChanged。您的视图模型也应该从中继承。让视图模型类继承INotifyPropertyChanged,并实现接口。然后将TxtResults属性替换为以下内容:
private string _txtResults = string.Empty;
public string TxtResults
{
get { return this._txtResults; }
set
{
this._txtResults= value;
this.RaisePropertyChangedEvent("TxtResults");
}
}
发布XAML和这些属性(txtResults和txtInput)的定义\
private string _txtResults = string.Empty;
public string TxtResults
{
get { return this._txtResults; }
set
{
this._txtResults= value;
this.RaisePropertyChangedEvent("TxtResults");
}
}