Page 1 of 1

ImageSource fallback only works when property is not registered.

Posted: 13 Feb 2024, 01:11
by Jarryd
I'd like my image to fall back to a static resource, if the string is null or empty in my noesis C++ project.
             <Image x:Name="IconImage"
                    Grid.Row="0"
                    Grid.RowSpan="2"
                    Grid.Column="0"
                    Grid.ColumnSpan="3"
                    Source="{Binding IconImageSource, FallbackValue={StaticResource FallbackOutfitImageSource}}"
                    Stretch="UniformToFill" />
It does this in Blend.

But when running in Noesis in my game, I get nothing appearing when I've bound to a valid view model, with an empty image source.

I'm using strings for my image sources. So there must be a converter here, that's not failing when the source can't be resolved or is empty.

Is this intended behaviour?

Re: ImageSource fallback only works when property is not registered.

Posted: 15 Feb 2024, 19:21
by sfernandez
The FallbackValue is used when the binding cannot be resolved (the property in the binding path can't be found for example).
If the binding was correctly evaluated but the return value was just null, then you should use the TargetNullValue property of the binding.

In the following xaml the second rectangle is filled with the Cyan color:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d">
  <Grid.Resources>
    <SolidColorBrush x:Key="defBrush" Color="Magenta"/>
    <SolidColorBrush x:Key="nilBrush" Color="Cyan"/>
  </Grid.Resources>
  <StackPanel>
    <Rectangle x:Name="rect1" Width="200" Height="100" Fill="Red"/>
    <Rectangle x:Name="rect2" Width="200" Height="100" Fill="{Binding Stroke, ElementName=rect1,
          FallbackValue={StaticResource defBrush},
          TargetNullValue={StaticResource nilBrush}}"/>
  </StackPanel>
</Grid>
Is this what you're looking for?

Re: ImageSource fallback only works when property is not registered.

Posted: 17 Feb 2024, 16:45
by Demond
Does TargetNullValue work correctly? Let's say I have a variable Name, my code returns an empty string in Name if it is not specified by the user, I bind <Run Text="{Binding Name, FallbackValue='Player Fall', TargetNullValue='Player Null' }" />. But I get Binding failed: Path=Name, Source=null(''), Target=Run(''), TargetProperty=Run.Text. The fallback value will be used. And accordingly empty text is returned. How it should work with a string value?

Re: ImageSource fallback only works when property is not registered.

Posted: 19 Feb 2024, 11:54
by sfernandez
String properties are a bit different in Noesis compared to WPF because in C# strings are reference values that can be null, but not in C++.
So if the binding path is correct and the property returns an empty string then the binding will just use that value (no FallbackValue or TargetNullValue).

If you need your string properties to also support null values in C++ you should use a Nullable<String>. Anyway I'm seeing that even with that our behavior is different regarding bindings and TargetNullValue, could you please report this in our bugtracker?

Re: ImageSource fallback only works when property is not registered.

Posted: 20 Feb 2024, 06:19
by Jarryd
Is this what you're looking for?
This is exactly what I was looking for!
I misunderstood the purpose of a fallback, and now it's working correctly. Thank you very much for clarifying!

Re: ImageSource fallback only works when property is not registered.

Posted: 20 Feb 2024, 17:40
by Demond
String properties are a bit different in Noesis compared to WPF because in C# strings are reference values that can be null, but not in C++.
So if the binding path is correct and the property returns an empty string then the binding will just use that value (no FallbackValue or TargetNullValue).

If you need your string properties to also support null values in C++ you should use a Nullable<String>. Anyway I'm seeing that even with that our behavior is different regarding bindings and TargetNullValue, could you please report this in our bugtracker?
I reported this on the bug tracker. Please correct if something is wrong.

Link: https://www.noesisengine.com/bugs/view.php?id=3103

Re: ImageSource fallback only works when property is not registered.

Posted: 22 Feb 2024, 10:54
by sfernandez
Thanks for the ticket!