0
我有一个文本框,我需要绑定一个字符串。WPF绑定问题
<TextBox Name="txtDoc" Margin="5" Text ="{Binding Source={x:Static local:DocumentViewModel.FileText}, Path=FileText}">
的FILETEXT属性在不同的类变化:
DocumentViewModel.GetInstance().FileText = File.ReadAllText(document.Path);
的DocumentViewModel是辛格尔顿一类:
public class DocumentViewModel : INotifyPropertyChanged
{
private static string fileText;
public string FileText
{
get { return fileText; }
set
{
fileText = value; // Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("FileText");
}
}
private void OnPropertyChanged(string filetext)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(filetext));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private static DocumentViewModel instance = new DocumentViewModel();
private DocumentViewModel() { }
public static DocumentViewModel GetInstance()
{
return instance;
}
}
我需要能够改变的价值FileText属性并在文本框中反映此更改。 这不起作用。 我尝试使用TextBox作为一个静态属性,但然后Onp
“我得到一个错误:“本地:DocumentViewModel'成员无效,因为它没有合格的类型名称 – Gil 2011-05-29 09:35:36
@Gil更新了我的答案 – Ben 2011-05-29 09:41:36
@Gil:这个实例需要作为公共财产公开,现在它是一个私人领域。 – 2011-05-29 10:31:13