Wpf Binding - Default Value for Empty String

WPF Binding - Default value for empty string

I was under the impression that FallbackValue provides a value when the binding fails and TargetNullValue provides a value when the bound value is null.

To do what you want you will either need a converter (possibly with a parameter) to convert an empty string to a target value, or put the logic in your view model.

I would probably go with a converter something like this (not tested).

public class EmptyStringConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty(value as string) ? parameter : value;
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

How to set a default empty binding unless the value is different from 0?

In C#, int is a value type, meaning that it can never be null. You can use the Nullable<int> to transform it as a Nullable, that defaults to null.

A shortcut is with the question mark.
If you define your Position as int? Position you should get the result you're after.

Edit:
Didn't see the request about the value disappearing after the Position = 0 again.

A good alternative would be to use a IValueConverter as proposed in the comments.

Otherwise, you could use a trigger, like so:

<Style TargetType="TextBlock"
Text="{Binding Position}">
<Style.Triggers>
<Trigger Property="Text" Value="0">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>

Set default value in XAML if binding is null

I found the answer to this problem in another question of mine (The code is from Clemens):

<Window.Resources>
<model:PathSelector x:Key="FallbackPathSelector" />
</Window.Resources>
...

<ContentControl Content="{Binding MyPathSelector,
FallbackValue={StaticResource FallbackPathSelector}}"/>

WPF: ListView: Display a default value instead of an empty string

You can modify the ListView ItemTemplate, and use an IValueConverter as such:

<ListView 
ItemsSource="{Binding AvailableTags}"
SelectedItem="{Binding SelectedTag}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource TagConverter}}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

The converter:

public class TagConverter : IValueConverter
{
#region IValueConverter Members

object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string tag = (string)value;

return string.IsNullOrEmpty(tag) ? "Your custom string display" : tag;
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

#endregion
}

Remember to add the converter as a resource in scope.

How to specify an empty string as Target null value in xaml

You can use x:Static in the xaml and define the string.empty there.

<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue={x:Static system:String.Empty} }" />

You'll need to add the appropriate namespace to the xaml as needed. VS should do this for you though. Namespace required is

xmlns:system="clr-namespace:System;assembly=mscorlib"

Binding an empty string if value is non-null in wpf

In case you want TextBlock to be shown only in case binding value is null, you can have trigger in place and set Visibility to Visible when binding value is null and otherwise Collapsed always.

<TextBlock Text="{Binding TargetNullValue=NullValue}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>

WPF Binding using a default value

Your converter should return Restrictive for the Fill property of the Ellipse to be set to Red. This will trigger your last DataTrigger:

<DataTrigger Binding="{Binding SignalStatus, Converter={StaticResource IntToSignalStatus} }" Value="Restrictive">
<Setter Property="Fill" Value="Red"/>
</DataTrigger>

Only if the converter returns neither Unknown, Permissive nor Restrictive, your default (uncommented) setter will apply.

Use a default value when binding cannot be evaluated because of a null value

You could use a PriorityBinding:

<TextBlock>
<TextBlock.Text>
<PriorityBinding>
<Binding Path="CurrentCustomer.Name" />
<Binding Source="---" />
</PriorityBinding>
</TextBlock.Text>
</TextBlock>

Okay, for Silverlight it is a probably easier to wrap the element in a wrapper (like a Border). You then have an IValueConverter to convert null to Visibility.Collapsed, and anything else to Visibility.Visible:

public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

And use it like so:

<Border Visibility="{Binding CurrentCustomer, Converter={StaticResource NullToVisibilityConverter}}">
<TextBlock Text="You have a car" Visibility="{Binding CurrentCustomer.HasACar,Converter={StaticResource boolToVisibilityConverter}}" />
</Border>


Related Topics



Leave a reply



Submit