我想制作一个简单的WPF应用程序,其中包含一个Button和一个TextBlock在视图中。我想,当我点击按钮时,向TextBlock写入“Hello”。TextBlock绑定不起作用
这是我的观点:
<Window x:Class="PropertyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="ButtonIncreaser" Content="Button" Command="{Binding CalculateCommand}" CommandParameter="+" HorizontalAlignment="Left" Height="23" Margin="400,206,0,0" VerticalAlignment="Top" Width="75"/>
<TextBlock Name="TextB" HorizontalAlignment="Left" Height="23" Margin="56,206,0,0" TextWrapping="Wrap" Text="{Binding Szoveg,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="292"/>
</Grid>
</Window>
那MainWindow.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel m = new ViewModel();
this.DataContext = m;
this.Show();
}
}
而这就是我的视图模型:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public DelegateCommand CalculateCommand { get; private set; }
public ViewModel()
{
CalculateCommand = new DelegateCommand(param => Calculate(param.ToString()));
}
public void Calculate(string param)
{
_str = "Hello";
}
private string _str;
private string Szoveg
{
get
{
return _str;
}
set
{
_str = value;
OnPropertyChanged("Szoveg");
}
}
public void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
我该怎么办了?谢谢!
您的'Szoveg'属性是'private'。需要公开。 – XAMlMAX