C# Wpf将文本块绑定到应用程序属性';s成员
在我的WPF应用程序中,我希望一个对象“CaseDetails”被全局使用,即被所有windows和用户控件使用。CaseDetails实现INotifyPropertyChanged,并具有属性CaseNameC# Wpf将文本块绑定到应用程序属性';s成员,c#,wpf,binding,global,textblock,C#,Wpf,Binding,Global,Textblock,在我的WPF应用程序中,我希望一个对象“CaseDetails”被全局使用,即被所有windows和用户控件使用。CaseDetails实现INotifyPropertyChanged,并具有属性CaseName public class CaseDetails : INotifyPropertyChanged { private string caseName, path, outputPath, inputPath; public CaseDetails() {
public class CaseDetails : INotifyPropertyChanged
{
private string caseName, path, outputPath, inputPath;
public CaseDetails()
{
}
public string CaseName
{
get { return caseName; }
set
{
if (caseName != value)
{
caseName = value;
SetPaths();
OnPropertyChanged("CaseName");
}
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
在我的App.xaml.cs中,我创建了一个CaseDetails对象
public partial class App : Application
{
private CaseDetails caseDetails;
public CaseDetails CaseDetails
{
get { return this.caseDetails; }
set { this.caseDetails = value; }
}
在我的一个用户控制代码中,我创建了CaseDetails对象并在App类中设置
(Application.Current as App).CaseDetails = caseDetails;
并更新App类的CaseDetails对象
在MainWindow.xml中,我有一个绑定到CaseDetails的CaseName属性的TextBlock。此文本块不更新。xml代码是:
<TextBlock Name="caseNameTxt" Margin="0, 50, 0, 0" FontWeight="Black" TextAlignment="Left" Width="170" Text="{Binding Path=CaseDetails.CaseName, Source={x:Static Application.Current} }"/>
为什么此文本块文本权限未得到更新??绑定中哪里出错了???绑定未更新,因为您正在应用程序类中设置
CaseDetails
属性,该属性未实现INotifyPropertyChanged
您还可以在应用程序类中实现INotifyPropertyChanged,或者只设置现有CaseDetails实例的属性:
(Application.Current as App).CaseDetails.CaseName = caseDetails.CaseName;
...
CaseDetails
属性可能是只读的:
public partial class App : Application
{
private readonly CaseDetails caseDetails = new CaseDetails();
public CaseDetails CaseDetails
{
get { return caseDetails; }
}
}
我尝试将案例细节设置为只读,并以这种方式工作,但没有任何区别。最后,我尝试在App类中实现INotifyPropertyChanged,这完成了绑定更新。谢谢