views:

381

answers:

1

I'm trying to bind a button command to take the text of a textbox as a parameter when the button is clicked. My Xaml looks like this:

<TextBox x:Name="InputBox" Width="250" TabIndex="1" 
   Text="{Binding Path=MessageText, Mode=TwoWay}" 
   FontFamily="Verdana" FontSize="11" Margin="0,0,4,0" />
<Button x:Name="SendButton" Width="50" Content="Send" TabIndex="2"
   commands:Click.CommandParameter="{Binding Path=MessageText}"
   commands:Click.Command="{Binding SendMessageCommand}" />

Where MessageText is defined like so:

private string mMessageText;
public string MessageText
{
     get { return mMessageText; }
     set { mMessageText = value; OnPropertyChanged(MessageText); }
}

And my DelegateCommand looks like so:

public ICommand SendMessageCommand { get; private set; }

public TestModuleViewModel()
{
     Messages = new ObservableCollection<Message>();
     this.SendMessageCommand = new DelegateCommand<string>(text =>
     {
          Messages.Add(CreateMessage(text, "Me"));
     });
}

I've run this with a breakpoint set at the delegate and the parameter 'text' is coming up null every time. If I replace the binding statement commands:Click.CommandParameter="{Binding Path=MessageText}" with some hard coded value (as in commands:Click.CommandParameter="Foo" ), I get the value as expected. What am I missing on the binding side?

+3  A: 

Unless you have something REALLY fancy going on with your OnPropertyChanged implementation, it's likely because this:

OnPropertyChanged(MessageText);

Should be this:

OnPropertyChanged("MessageText");

Hope this helps.

Anderson Imes