WPF Binding INotifyPropertyChanged 다중 스레드 이해
7596 단어 property
Person.cs
public class Person : ObservableObject,INotifyPropertyChanged
{
private string _testName;
private ObservableCollection<string> _names=new ObservableCollection<string>();
public string TestName
{
get
{
return _testName;
}
set
{
_testName = value;
OnPropertyChanged();
}
}
public ObservableCollection<string> Names
{
get { return _names; }
set
{
_names = value;
RaisePropertyChanged(()=>Names);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MainWindow.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
private int _currentId;
private Person _person;
public Person Person
{
get
{
return _person;
}
set
{
_person = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Person = new Person();
Person.TestName = "TestName";
Person.Names.Add("string");
_currentId = Thread.CurrentThread.ManagedThreadId;
DispatcherHelper.Initialize();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
if (_currentId == Thread.CurrentThread.ManagedThreadId)
{
Person = new Person();
}
else
{
// ?
Person.TestName = "NotSame";
// ?
Person.Names.Add("Hello");
}
});
}
}
주석에 주의하다
결과적으로TestName 속성은 UI에 올바르게 업데이트될 수 있지만 집합 속성Names는 그렇지 않습니다.
나머지 의 이해 는 한 편 은 잘 썼다
http://www.cnblogs.com/wpcockroach/p/3909081.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python 특수 속성property 원리 및 사용 방법 분석1 특성 속성 property는 특수한 속성입니다. 접근할 때 기능 (함수) 을 실행하고 값을 되돌려줍니다. 주의: 이 때의 특성arear와perimeter는 값을 부여할 수 없습니다 2 왜 프로퍼티를 써? 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.