我正在尝试绑定一个按钮命令,以便在单击按钮时将文本框的文本作为参数。我的 Xaml 看起来像这样:
<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}" />
MessageText 的定义如下:
private string mMessageText;
public string MessageText
{
get { return mMessageText; }
set { mMessageText = value; OnPropertyChanged(MessageText); }
}
我的 DelegateCommand 看起来像这样:
public ICommand SendMessageCommand { get; private set; }
public TestModuleViewModel()
{
Messages = new ObservableCollection<Message>();
this.SendMessageCommand = new DelegateCommand<string>(text =>
{
Messages.Add(CreateMessage(text, "Me"));
});
}
我已经在委托处设置了一个断点来运行它,并且参数“文本”每次都为空。commands:Click.CommandParameter="{Binding Path=MessageText}"
如果我用一些硬编码的值(如)替换绑定语句 commands:Click.CommandParameter="Foo"
,我会得到预期的值。我在绑定方面缺少什么?