Binding.TargetNullValue property

Applies to Windows and Windows Phone

Gets or sets the value that is used in the target when the value of the source is null.

Syntax


public object TargetNullValue { get; set; }


<Binding TargetNullValue="nullValueString" />



<Binding>
  <Binding.TargetNullValue>
    nullValue
  </Binding.TargetNullValue>
</Binding>


XAML Values

nullValueString

A string that can be converted to provide a value for the Binding target in XAML, in cases where the source was null.

nullValue

An object element instance that can provide a value for the Binding target in XAML, in cases where the source was null.

Property value

Type: System.Object [.NET] | Platform::Object [C++]

The value that is used in the binding target when the value of the source is null.

Remarks

TargetNullValue might be used for bindings that bind a collection and the source data uses null for missing info only in some of the items. It might also be used for general cases where the data is coming from a database that uses null values as source info to signify something, such as a record that needs more info from the user and isn't complete in the source.

There are two recommended patterns for using TargetNullValue behavior in a Binding:

  • The binding source provides a separate value that is accessed by a different path, which acts as the singleton value that can substitute for any null value coming from a specific data item in the source. For example:
    
    <Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={Binding Path=NullValue}}" />
    
    
    Here, NullValue is the path to the substitute value, and is referenced by another Binding.
  • Use resources to provide a value that's specific to your app in cases where the data source provided null and has no suitable property in another path to use as the substitution value. For example,
    
    <Button Content="{Binding Path=NextItem, Mode=OneWay, TargetNullValue={StaticResource AppStringForNullInAStringABinding}}" />
    
    
    Here, AppStringForNullInAStringABinding is a resource string something like "(value not available)" that lets the user know that there should be data there in most of the data but this particular item didn't have that data from its source. Use resources so that the value can be localized.

FallbackValue is a similar property with similar scenarios. The difference is that a binding uses FallbackValue for cases where the Path and Source doesn't evaluate on the data source at all, or if attempting to set it on the source with a two-way binding throws an exception. It uses TargetNullValue if the Path does evaluate, but the value found there is null.

You can't set the property values of a Binding object after that binding has been attached to a target element and target property. If you attempt this you'll get a run-time exception.

Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Data
Windows::UI::Xaml::Data [C++]

Metadata

Windows.winmd

See also

Binding
FallbackValue
XAML data binding sample
Data binding overview

 

 

Show:
© 2014 Microsoft