0

I'd like some help with my test project. I'd like when I press one of my buttons and the letter consists in the hidden word at the textbox(hangman) to update the textbox with the revealed letter/s. Currently my logic with the guessing works but the textbox won't update.

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));
        }
    }
}
9
  • Do you have any binding errors, pls check the output window of your visual studio, Everything seems to be fine for me Commented Apr 19, 2016 at 12:16
  • ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== Commented Apr 19, 2016 at 12:22
  • not this, while running the project , if there any binding error it will show in output Commented Apr 19, 2016 at 12:22
  • i don't see any erros. It's saying that some stuff are loaded and: Step into: Stepping over non-user code 'test.App.Main' Step into: Stepping over non-user code 'test.App.InitializeComponent' Commented Apr 19, 2016 at 12:27
  • The binding seems to be working fine , i replicated the code. pls show us what you doing in the button click Commented Apr 19, 2016 at 12:34

2 Answers 2

2

You are setting the private field "displayWordInTextbox" instead of the binded property "DisplayWordInTextbox" so the NotifyPropertyChanged is not triggered.

Replace "displayWordInTextbox" with "DisplayWordInTextbox" inside the displayWord function and should work.

private void displayWord()
{
    DisplayWordInTextbox = "";
    for (int i = 0; i < copyCurrentWord.Length; i++)
    {
        DisplayWordInTextbox += copyCurrentWord.Substring(i, 1);
        DisplayWordInTextbox += " ";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

here is my entire 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 += " ";
        }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.