开发者

WPF: Is there a way to bind to a Nullable<Int32> property without using a value converter?

Is there a way to bind to a Nullable property without using a value converter?开发者_StackOverflow社区

Currently I have this...

<DataGridTextColumn Header="ApplicationKey" Binding="{Binding ApplicationKey, ValidatesOnDataErrors=True, Converter={StaticResource ResourceKey=TestConverter}}" />

Without the converter it thinks of an empty textbox as a String.Empty instead of a Null.

I'm hoping there is some sort of magic property like TargetNullValue.


Yes, the TargetNullValue property on Binding should do exactly what you want. If you set TargetNullValue to the empty string, then the binding will convert the empty string to null and back:

<DataGridTextColumn Header="ApplicationKey" Binding=
    "{Binding ApplicationKey, ValidatesOnDataErrors=True, TargetNullValue=''}" />


So let me get this straight... you want the text to be bound to null instead of string.empty?

If this is the case, instead of binding via the converter, you COULD bind to a property in a view model (if you dont have a viewmodel, you can create it and bind your xaml to it).

That property in question could then return (in the getter section of the property) a null value rather than a string.empty.

If this doesn't solve your problem please describe in more detail and why you want to bind to a null instead of a string.empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜