Search code examples
c#wpfdata-bindingfallbackvalue

Is there a Fallback for Dataitem=null


I'm trying to resolve performance issues. The current gremlin is my tooltip that throws:

System.Windows.Data Information: 41 : BindingExpression path error: 'ViewLine3' property not found for 'object' because data item is null.  This could happen because the data provider has not produced any data yet. BindingExpression:Path=ViewLine3; DataItem=null; target element is 'TextBlock' (Name='line3ToolTip'); target property is 'Text' (type 'String')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=ViewLine3; DataItem=null; target element is 'TextBlock' (Name='line3ToolTip'); target property is 'Text' (type 'String')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=ViewLine3; DataItem=null; target element is 'TextBlock' (Name='line3ToolTip'); target property is 'Text' (type 'String')

For each item that implements it. I've tried to silence it by setting FallbackValue, TargetNullValue, Delay, IsAsync but the issue persists.

<StackPanel.ToolTip>
    <ToolTip>

        <StackPanel x:Name="suiteTooltip" 
                    Width="auto">
            <TextBlock  x:Name="line3ToolTip" 
                        Text="{Binding ViewLine3, 
                        FallbackValue='NoData', 
                        TargetNullValue='NoData', 
                        Delay=500, 
                        IsAsync=True}"/>
        </StackPanel>
    </ToolTip>
</StackPanel.ToolTip>

Is there another fallback that I don't know about that would allow me to handle the exceptions generated by the tooltip.

Note: The information is still being displayed correctly on screen. Only on creation (when I change a model that changes the view to create an element that has this tooltip) do these errors appear.


Solution

  • You can use a DataTrigger to handle this event. Note that FallBackValue is used when binding fails, in your situation it doesn't (it finds the property), which is why you see it not working.

    <TextBlock>
      <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Text" Value="{Binding ViewLine3, FallbackValue='NoData', TargetNullValue='NoData', 
                        Delay=500, IsAsync=True}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ViewLine3}" Value="{x:Null}">
                        <Setter Property="Text" Value="NoData"/>
                    </DataTrigger>
                      <DataTrigger Binding="{Binding Path=ViewLine3.Length, FallbackValue=0, TargetNullValue=0}" Value="0">
                        <Setter Property="Text" Value="NoData"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>