views:

42

answers:

1

Greetings guys, hopefully somebody has fresher eyes and can help me pinpoint the problem here, I'm trying to create a small app with prism and the MVVM pattern, everything was working nicely up to this point, my commands are firing properly with the argument, however, the TextBlock here is not binding to the CurrentUserKey property from it's viewmodel as it should.

Anybody has any ideas? thanks in advance...

LoginView.xaml (only relevant parts for brevity) ...

<Grid DataContext="{Binding Path=., Source={StaticResource viewModel}}">
  <Grid Margin="10">
    <Label  VerticalAlignment="Center" HorizontalAlignment="Right">Enter your Key:</Label>
    <TextBlock Name="txtUserKey" Text="{Binding Path=CurrentUserKey}" Margin="2" />

    <Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="7">7</Button>
    <Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="8">8</Button>
    <Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="9">9</Button>
...
  </Grid>
...

LoginViewModel.cs

public class LoginViewModel : ViewModelBase
    {
        public LoginViewModel()
        {
            GenericButtonClick = new DelegateCommand<string>(GenericButtonClickHandler);
        }

        private void GenericButtonClickHandler(string argument)
        {
            if (argument.Length < 2) {
                CurrentUserKey += argument;
            }

            RaisePropertyChangedEvent("GenericButtonClick");
        }

        public string CurrentUserKey { get; set; }
        private ICommand GenericButtonClick { get; set; }
    }

ViewModelBase.cs

public class ViewModelBase:INotifyPropertyChanged   
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string Property_name)
    {
        if (Property_name == null) return;

        PropertyChangedEventArgs e = new PropertyChangedEventArgs(Property_name);
        PropertyChanged(this, e);
    }
}
+1  A: 

You are not raising PropertyChanged when CurrentUserKey has changed.

Additionally, there are some issues with binding to Text in a TextBox: See http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/ and http://stackoverflow.com/questions/1648792/wpf-textbox-text-is-not-updating

Daniel Rose
Thanks for answering Daniel, I'll start reading on the Textbox issue now, I thought It was raising The PropertyChanged here RaisePropertyChangedEvent("GenericButtonClick") from the ViewModelBase class which in turns calls PropertyChanged; no? super possible I'm missing something here (i'm a total n00b)
Geo
You have to raise PropertyChanged for the property which changed. So in your setter of CurrentUserKey, you have to call RaisePropertyChangedEvent("CurrentUserKey");
Daniel Rose
aaaaaaaaaaaaargggghhhh, you're absolutely right maaan, I was raising the wrong property! (You can raise it on the ExecuteMethodHandler as well, but to avoid duplication is better with your suggestion)... thanks man.
Geo