为什么我的命令参数绑定不正确?
我正在尝试绑定一个按钮命令,以在单击按钮时将文本框的文本作为参数。我的 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"));
});
}
我已经在委托处设置了断点来运行它,并且参数“text”每次都为空。如果我将绑定语句 commands:Click.CommandParameter="{Binding Path=MessageText}"
替换为一些硬编码值(如 commands:Click.CommandParameter="Foo"
> ),我得到了预期的值。我在装订方面缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您对 OnPropertyChanged 实现有真正的兴趣,否则很可能是因为:
应该是这样:
希望这会有所帮助。
Unless you have something REALLY fancy going on with your OnPropertyChanged implementation, it's likely because this:
Should be this:
Hope this helps.