WPF绑定一个TextBlock到App物业的成员 [英] Wpf Binding a TextBlock to App Property's member
问题描述
在我的WPF应用程序,我想一个对象CaseDetails予以即通过所有窗口和放大器全球范围内使用;用户控件。 CaseDetails实现INotifyPropertyChanged,并有一个属性CaseName。
In my WPF app, I want an object "CaseDetails" to be used globally i.e. by all windows & user controls. CaseDetails implements INotifyPropertyChanged, and has a property CaseName.
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的对象
In my App.xaml.cs, I created an object of CaseDetails
public partial class App : Application
{
private CaseDetails caseDetails;
public CaseDetails CaseDetails
{
get { return this.caseDetails; }
set { this.caseDetails = value; }
}
在我的用户控制code后面的一个,我创建CaseDetails的对象,并在App类设置
In one of my user control code behind, I create object of CaseDetails and set in App class
(Application.Current as App).CaseDetails = caseDetails;
和App类的CaseDetails对象被更新。
And the CaseDetails object of App class is updated.
在我MainWindow.xml,我有一个绑定到CaseName CaseDetails财产一个TextBlock。这个文本块不会更新。该XML code是:
In my MainWindow.xml, I have a TextBlock that is bind to CaseName property of CaseDetails. This Textblock doesn't update. The xml code is :
<TextBlock Name="caseNameTxt" Margin="0, 50, 0, 0" FontWeight="Black" TextAlignment="Left" Width="170" Text="{Binding Path=CaseDetails.CaseName, Source={x:Static Application.Current} }"/>
为什么这样的TextBlock文本poperty是没有得到更新?我要去哪里错了??绑定
Why this TextBlock Text poperty is not getting updated ?? Where am I going wrong in Binding ??
推荐答案
绑定没有更新,因为你在你的App类,它不设置 CaseDetails
属性不实现INotifyPropertyChanged。
The binding isn't updated because you are setting the CaseDetails
property in your App class, which does not implement INotifyPropertyChanged.
要么你也实现INotifyPropertyChanged在你的应用程序类,或者你刚刚设置的现有CaseDetails实例的属性:
Either you also implement INotifyPropertyChanged in your App class, or you just set properties of the existing CaseDetails instance:
(Application.Current as App).CaseDetails.CaseName = caseDetails.CaseName;
...
的 CaseDetails
属性可能再是只读的:
The CaseDetails
property might then be readonly:
public partial class App : Application
{
private readonly CaseDetails caseDetails = new CaseDetails();
public CaseDetails CaseDetails
{
get { return caseDetails; }
}
}
这篇关于WPF绑定一个TextBlock到App物业的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!