数据绑定似乎没有刷新 [英] Databindings don't seem to refresh
问题描述
我试图将一个文本框绑定到一个字符串,该程序的输出活动。我为字符串创建了一个属性,但是当属性更改时,文本框不会。我有一个列表视图的这个问题,但创建了一个刷新列表视图的调度程序。
我一定缺少一些要点,因为我认为使用wpf的一个好处是不需要手动更新控件。我希望有人可以向我正确的方向发送。
在windowMain.xaml.cs
private string debugLogText =initial value;
public String debugLog {
get {return debugLogText; }
set {debugLogText = value;
}
在windowMain.xaml
x:Name =wndowMain
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns :x =http://schemas.microsoft.com/winfx/2006/xaml
DataContext ={Binding RelativeSource = {RelativeSource Self}}
< TextBox Name = txtDebugText ={Binding ElementName = wndowMain,Path = debugLog}/>
在你的课上实现INotifyPropertyChanged。如果你有很多类需要这个接口,我经常会发现使用如下的基类很有帮助。
public abstract class ObservableObject:INotifyPropertyChanged
{
protected ObservableObject()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = PropertyChanged;
if(handler!= null){
handler(this,e);
}
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
然后你只需要确保只要属性值更改,就会引发PropertyChanged事件。例如:
public class Person:ObservableObject {
private string name;
public string Name {
get {
return name;
}
set {
if(value!= name){
name = value;
OnPropertyChanged(Name);
}
}
}
}
For some reason I'm really struggling with this. I'm new to wpf and I can't seem to find the information I need to understand this simple problem.
I am trying to bind a textbox to a string, the output of the programs activity. I created a property for the string, but when the property changes, the textbox does not. I had this problem with a listview, but created a dispatcher which refreshes the listview.
I must be missing some major point, because I thought one benefit of using wpf was not having to update controls manually. I hope someone can send me in the right direction.
in windowMain.xaml.cs
private string debugLogText = "initial value";
public String debugLog {
get { return debugLogText; }
set { debugLogText = value; }
}
in windowMain.xaml
x:Name="wndowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
<TextBox Name="txtDebug" Text="{Binding ElementName=wndowMain, Path=debugLog}" />
Implement INotifyPropertyChanged on your class. If you have many classes that need this interface, I often find it helpful to use a base class like the following.
public abstract class ObservableObject : INotifyPropertyChanged
{
protected ObservableObject( )
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
{
var handler = PropertyChanged;
if ( handler != null ) {
handler( this, e );
}
}
protected void OnPropertyChanged( string propertyName )
{
OnPropertyChanged( new PropertyChangedEventArgs( propertyName ) );
}
}
Then you just have to make sure you raise the PropertyChanged event whenever a property value changes. For example:
public class Person : ObservableObject {
private string name;
public string Name {
get {
return name;
}
set {
if ( value != name ) {
name = value;
OnPropertyChanged("Name");
}
}
}
}
这篇关于数据绑定似乎没有刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!