2016-04-20 14 views
0

Ich habe eine Hausaufgabe für einen Henker Spiel. Ich möchte in der buttonFunction oder anderswo, die Taste, die ich gedrückt habe, um Versteckt und so weiter zu kommen.C# MVVM Tasten ausblenden, die Sie geklickt haben

Mainwindow:

<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:

using System; 
using System.ComponentModel; 
using System.Linq; 
using System.Runtime.CompilerServices; 
using System.Windows; 
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(); 
     } 
     else 
     { 
      System.Windows.MessageBox.Show(buttonContent + " isn't found!"); 
     } 
    } 

    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 += " "; 
     } 
    } 
    } 
} 
+0

Wollen Sie wirklich, dass die Schaltfläche ** unsichtbar ** wird, wenn Sie darauf klicken? Da das Standardverhalten bei der Befehlsbindung ist, wird eine Schaltfläche ** inaktiv **, wenn der Befehl "CanExecute()' '' false "zurückgibt. Dann würden Sie keine zusätzlichen Eigenschaften im ViewModel benötigen. –

Antwort

0

Sie können

für jede Schaltfläche Sichtbarkeit Eigenschaft erstellen

**. XAML

<Button Grid.Column="0" Grid.Row="0" Command="{Binding ButtonClick}" Visibility="{Binding MyProperty}" CommandParameter="a"> 
     a 
</Button> 

**. Cs

private Visibility myProperty 
public Visibility MyProperty 
{ 
    get {return myProperty;} 
    set {myProperty = value;NotifyPropertyChanged("MyProperty");} 
} 

und in Ihrem Befehl, können Sie die Sichtbarkeit wechseln. So etwas wie das:

void buttonFunction(object obj) 
{ 
    switch(obj.ToString()) 
    { 
     case "a": 
     MyPropertyA = Visibility.Hidden; 
     MyPropertyB = Visibility.Visible; 
     break; 
     case "b": 
     MyPropertyB = Visibility.Hidden; 
     break; 
     .... 
    } 
    .... 
} 
+0

Wenn ich das tue alle meine Tasten gehen Versteckt, nicht nur die angeklickte – coolside

+0

Sie müssen Eigenschaft für jede Schaltfläche erstellen. – Amine

+0

und wer könnte ich wissen, welche Eigenschaft in der buttonFunction aktiviert werden muss? – coolside

Verwandte Themen