2011-05-29 101 views
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

回答

2

尝试将源代码设置为您的viewmodel而不是属性本身,并将instance属性设置为public? {Binding Source={x:Static local:DocumentViewModel.instance}, Path=FileText}

编辑:包括一个完整的例子,这工作对我来说:

的XAML:

<Window x:Class="Test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Test" 
    Title="MainWindow" Height="350" Width="525" 
    Loaded="Window_Loaded"> 
    <TextBox Name="txtDoc" Margin="5" 
      Text="{Binding Source={x:Static local:DocumentViewModel.Instance}, Path=FileText}" /> 
</Window> 

代码隐藏:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
    InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
    DocumentViewModel.Instance.FileText = "Hello world!";  
    } 
} 

public class DocumentViewModel : INotifyPropertyChanged 
{ 
    #region Singleton implementation 

    // Static constructor to create the singleton instance. 
    static DocumentViewModel() 
    { 
    DocumentViewModel.Instance = new DocumentViewModel(); 
    } 

    public static DocumentViewModel Instance { get; private set; } 

    #endregion 

    private static string fileText; 
    public string FileText 
    { 
    get { return fileText; } 
    set 
    { 
     if (fileText != value) 
     { 
     fileText = value; 
     OnPropertyChanged("FileText"); 
     } 
    } 
    } 

    #region INotifyPropertyChanged 

    private void OnPropertyChanged(string filetext) 
    { 
    PropertyChangedEventHandler handler = PropertyChanged; 
    if (handler != null) 
    { 
     handler(this, new PropertyChangedEventArgs(filetext)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 
+0

“我得到一个错误:“本地:DocumentViewModel'成员无效,因为它没有合格的类型名称 – Gil 2011-05-29 09:35:36

+0

@Gil更新了我的答案 – Ben 2011-05-29 09:41:36

+0

@Gil:这个实例需要作为公共财产公开,现在它是一个私人领域。 – 2011-05-29 10:31:13