Dela via


Binding.TargetNullValue Property

Definition

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

public:
 property Platform::Object ^ TargetNullValue { Platform::Object ^ get(); void set(Platform::Object ^ value); };
IInspectable TargetNullValue();

void TargetNullValue(IInspectable value);
public object TargetNullValue { get; set; }
var object = binding.targetNullValue;
binding.targetNullValue = object;
Public Property TargetNullValue As Object
<Binding TargetNullValue="nullValueString" />
- or -
<Binding>
  <Binding.TargetNullValue>
    nullValue
  </Binding.TargetNullValue>
</Binding>

Property Value

Object

Platform::Object

IInspectable

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.

Applies to

See also