【问题标题】:How to set default value in wpf binding?如何在 wpf 绑定中设置默认值?
【发布时间】:2012-08-20 11:58:09
【问题描述】:

我有一个绑定到属性(数据类型为 int 的 CustomerId)的文本框。这个文本框最初显示一个 0(零),但我想要一个没有输入的清晰文本框。

我还将其默认值设置为 null,如下面的代码:

ValueProperty = DependencyProperty.Register(
            "Value",
            typeof(int?), typeof(ChooseCustomerControl),
            new FrameworkPropertyMetadata(null));

我怎样才能做到这一点?

【问题讨论】:

  • CustomerIdValue 有什么关系?我们说的是同一个属性吗?
  • Value={绑定路径=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
  • 但是您说您有一个“绑定到属性 CustomerId 的文本框”。那我猜你绑定TextBox.Text。那么Value 是干什么用的?
  • 或者Value 可能是派生文本框的属性?

标签: wpf data-binding binding


【解决方案1】:

您需要将 CustomerId 的数据类型从 int 更改为 int?。

如果您不能这样做,请创建 converter ,它将 CustomerId (int) 转换为想要的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2023-03-26
    • 2011-01-15
    • 2010-11-08
    • 2011-07-22
    • 2018-11-25
    • 2017-12-03
    相关资源
    最近更新 更多