为什么我的命令参数绑定不正确?

发布于 2024-08-06 08:24:25 字数 1231 浏览 12 评论 0原文

我正在尝试绑定一个按钮命令,以在单击按钮时将文本框的文本作为参数。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-08-13 08:24:25

除非您对 OnPropertyChanged 实现有真正的兴趣,否则很可能是因为:

OnPropertyChanged(MessageText);

应该是这样:

OnPropertyChanged("MessageText");

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文