2011-05-29 99 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