如何在wpf绑定中设置默认值?

时间:2012-08-20 11:58:10

标签: wpf data-binding binding

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

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

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

我怎样才能实现这一目标?

1 个答案:

答案 0 :(得分:1)

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

如果您不能这样做,请创建converter ,将CustomerId(int)转换为所需值。