2016-04-19 56 views
0

我想幫助一下我的測試項目。當我按下其中一個按鈕時,我想要在文本框(hang子手)的隱藏文字中包含該字母,以便使用顯示的字母更新文本框。目前我的猜測作品的邏輯,但文本框不會更新。c#wpf MVVM文本框更新

MainWindow.xaml: 
<Window x:Class="test.MainWindow" 
    xmlns:vm="clr-namespace:test.ViewModel" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

<Window.DataContext> 
    <vm:MainWindowViewModel /> 
</Window.DataContext> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="59*"/> 
     <RowDefinition Height="55*"/> 
     <RowDefinition Height="68*"/> 
     <RowDefinition Height="65*"/> 
     <RowDefinition Height="72*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="13*"/> 
     <ColumnDefinition Width="34*"/> 
    </Grid.ColumnDefinitions> 
    <Button Grid.Column="0" Grid.Row="0" Command="{Binding ButtonClick}" CommandParameter="a"> 
     a 
    </Button> 
    <Button Grid.Column="0" Grid.Row="1" Command="{Binding ButtonClick}" CommandParameter="b"> 
     b 
    </Button> 
    <Button Grid.Column="0" Grid.Row="2" Command="{Binding ButtonClick}" CommandParameter="c"> 
     c 
    </Button> 
    <Button Grid.Column="0" Grid.Row="3" Command="{Binding ButtonClick}" CommandParameter="d"> 
     d 
    </Button> 
    <Button Grid.Column="0" Grid.Row="4" Command="{Binding ButtonClick}" CommandParameter="e"> 
     e 
    </Button> 
    <TextBox Text="{Binding Path=DisplayWordInTextbox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2"/> 
</Grid> 

MainWindowViewModel.cs:

class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private string displayWordInTextbox; 
    public string DisplayWordInTextbox 
    { 
     get 
     { 
      return displayWordInTextbox; 
     } 
     set 
     { 
      displayWordInTextbox = value; 
      NotifyPropertyChanged("DisplayWordInTextbox"); 
     } 
    } 

    public MainWindowViewModel() 
    { 
     buttonClick = new RelayCommand(buttonFunction); 
     loadWordsFromFile(); 
     selectWord(); 
     displayWord(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
} 
+0

你有任何綁定錯誤,請檢查您的Visual Studio的輸出窗口, 一切都似乎是爲我好 – Eldho

+0

==========生成:1成功,0失敗,0最新,0跳過========== – tony

+0

不是這個,在運行項目時,如果有任何綁定錯誤,它會顯示在輸出 – Eldho

回答

2

要設置私有字段 「displayWordInTextbox」,而不是綁定的屬性「 DisplayWordInTextbox「,所以NotifyPropertyChanged沒有被觸發。

將displayWordInTextbox替換爲displayWord函數內的「DisplayWordInTextbox」,並且應該可以工作。

private void displayWord() 
{ 
    DisplayWordInTextbox = ""; 
    for (int i = 0; i < copyCurrentWord.Length; i++) 
    { 
     DisplayWordInTextbox += copyCurrentWord.Substring(i, 1); 
     DisplayWordInTextbox += " "; 
    } 
} 
+0

謝謝!它現在工作:) – tony

0

這裏是我的整個MainWindowViewModel:

using System; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Runtime.CompilerServices; 
using System.Windows.Input; 

namespace test.ViewModel 
{ 
class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private string[] words; 
    private string currentWord; 
    private string copyCurrentWord; 

    private string displayWordInTextbox; 
    public string DisplayWordInTextbox 
    { 
     get 
     { 
      return displayWordInTextbox; 
     } 
     set 
     { 
      displayWordInTextbox = value; 
      NotifyPropertyChanged("DisplayWordInTextbox"); 
     } 
    } 

    public MainWindowViewModel() 
    { 
     buttonClick = new RelayCommand(buttonFunction); 
     loadWordsFromFile(); 
     selectWord(); 
     displayWord(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

    private ICommand buttonClick; 
    public ICommand ButtonClick 
    { 
     get 
     { 
      return buttonClick; 
     } 
     set 
     { 
      buttonClick = value; 
     } 
    } 

    void buttonFunction(object obj) 
    { 
     string buttonContent = obj.ToString(); 
     if (currentWord.Contains(buttonContent) || currentWord.Contains(buttonContent.ToUpper())) 
     { 
      char[] temp = copyCurrentWord.ToCharArray(); 
      char[] findWord = currentWord.ToCharArray(); 
      char guessChar = buttonContent.ElementAt(0); 
      for (int i = 0; i < findWord.Length; i++) 
      { 
       if (findWord[i] == guessChar || findWord[i] == Char.ToUpper(guessChar)) 
       { 
        temp[i] = findWord[i]; 
       } 

      } 
      copyCurrentWord = new string(temp); 
      displayWord(); 
     } 
    } 

    private void loadWordsFromFile() 
    { 
     words = new string [] {"cat", "dog"}; 
    } 

    private void selectWord() 
    { 
     int randomWordIndex = (new Random()).Next(words.Length); 
     currentWord = words[randomWordIndex]; 
     char[] currentWordArray = currentWord.ToArray(); 
     bool isWord = false; 

     for (int i = 0; i < currentWord.Length; i++) 
     { 
      for (char c = 'a'; c <= 'z'; c++) 
      { 
       if (currentWordArray[i] == c || currentWordArray[i] ==  Char.ToUpper(c)) 
       { 
        isWord = true; 
       } 
      } 
      if (isWord == true) 
      { 
       copyCurrentWord += "_"; 
       isWord = false; 
      } 
      else 
      { 
       copyCurrentWord += currentWordArray[i]; 
      } 
     } 
     words = words.Where(w => w != words[randomWordIndex]).ToArray(); 
    } 

    private void displayWord() 
    { 
     displayWordInTextbox = ""; 
     for (int i = 0; i < copyCurrentWord.Length; i++) 
     { 
      displayWordInTextbox += copyCurrentWord.Substring(i, 1); 
      displayWordInTextbox += " "; 
     } 
    } 
    } 
}