C# 不更新UI的依赖项属性
我对绑定概念还不熟悉,并被以下内容困住了C# 不更新UI的依赖项属性,c#,data-binding,dependency-properties,C#,Data Binding,Dependency Properties,我对绑定概念还不熟悉,并被以下内容困住了 public sealed partial class MainPage : Page { Model model; public MainPage() { this.InitializeComponent(); model = new Model(); this.DataContext = model; } private void Button_Click
public sealed partial class MainPage : Page
{
Model model;
public MainPage()
{
this.InitializeComponent();
model = new Model();
this.DataContext = model;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
model.Name = "My New Name";
}
}
class Model : DependencyObject
{
public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Model), new PropertyMetadata("My Name"));
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
}
我已将Name属性绑定到TextView的Text属性。我所需要做的就是,在按钮上单击我想要更新名称值,该名称值必须更新文本框值。我想,如果我使用dependency属性而不是普通的CLR属性,我就不需要实现INotifyPropertyChanged
但UI中的值未按预期更新。我错过什么了吗
提前感谢。您的问题中有几件事需要解决。首先,您的模型不需要从DependencyObject继承,而是应该实现INotifyPropertyChanged:
public class Model : INotifyPropertyChanged
{
string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
NotifyPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后,可以将实现INotifyProperty的对象用作页面/窗口/对象中的DependencyProperty:
public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model",
typeof(Model), typeof(MainWindow));
public Model Model
{
get { return (Model)GetValue(ModelProperty); }
set { SetValue(ModelProperty, value); }
}
最后,您可以将TextBox.Text属性绑定到XAML中的:
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding Name}"/>
<Button Click="Button_Click">Click</Button>
</StackPanel>
</Grid>
点击
INotifyPropertyChanged在这里仍然是必需的,因为需要有一种方法让UI知道模型对象已经更新。您的问题需要解决一些问题。首先,您的模型不需要从DependencyObject继承,而是应该实现INotifyPropertyChanged:
public class Model : INotifyPropertyChanged
{
string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
NotifyPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后,可以将实现INotifyProperty的对象用作页面/窗口/对象中的DependencyProperty:
public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model",
typeof(Model), typeof(MainWindow));
public Model Model
{
get { return (Model)GetValue(ModelProperty); }
set { SetValue(ModelProperty, value); }
}
最后,您可以将TextBox.Text属性绑定到XAML中的:
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding Name}"/>
<Button Click="Button_Click">Click</Button>
</StackPanel>
</Grid>
点击
INotifyPropertyChanged在这里仍然是必需的,因为需要有一种方法让UI知道模型对象已经更新。WPF中没有TextView这样的控件。这是什么控件?这是Windows metro应用程序而不是wpf。请显示绑定表达式。wpf中没有TextView这样的控件。这是什么控件?这是Windows metro应用程序而不是wpf。请显示绑定表达式。