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