xaml中的变量与ViewModel中的公用属性绑定,当修改了公用属性时,xaml中绑定的值也会随之变化。
注意:不是绑定字段。(this.Name = "hh"; 正确,触发通知) (this.name= "hh"; 错误,不会触发通知)
using System.Windows;
namespace PropertyChange
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
}
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace PropertyChange
{
public class ViewModel : BindableBase
{
public string name = "TT";
public string Name
{
get { return name; }
set
{
SetProperty(ref name, value);
}
}
#region NewCommand
DelegateCommand<object> clickCommand = null;
public ICommand ClickCommand
{
get
{
if (clickCommand == null)
{
clickCommand = new DelegateCommand<object>((p) => OnClick(p), (p) => CanClick(p));
}
return clickCommand;
}
}
private bool CanClick(object parameter)
{
return true;
}
private void OnClick(object parameter)
{
this.Name = "hh";
}
#endregion
}
}