New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converter receiving unexpected null
value
#16016
Comments
This seems to be a problem not with ComboBox but something a lot deeper. I've followed the traces and managed to reduce it to this. <Page.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Page.Resources>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Click="Button_Click">Set</Button>
<Button Click="Button_Click_1">Reset</Button>
<ContentPresenter x:Name="cp">
<ContentPresenter.ContentTemplate>
<DataTemplate x:DataType="local:TestItem">
<TextBlock Text="Test" Visibility="{x:Bind Test, Converter={StaticResource BoolToVisibilityConverter}}" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</StackPanel> private void Button_Click(object sender, RoutedEventArgs e)
{
cp.Content = new TestItem();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
cp.Content = null;
} And then even further to: <Page.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Page.Resources>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Click="Button_Click">Set</Button>
<Button Click="Button_Click_1">Reset</Button>
<TextBlock x:Name="tb" Text="Test" Visibility="{Binding Test, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel> private void Button_Click(object sender, RoutedEventArgs e)
{
tb.DataContext = new TestItem();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
tb.DataContext = null;
} Which clears any doubts about lifecycle-related problems. If you very explicitly set the DataContext of an element to null, WinUI will still not call the converter with null. |
More updates: it seems like the converter can be called with null, but only if the final property of the path is null. I.e. if you have a bindinging to
|
I noticed that I forgot to go back and test my fix for x:Bind (I assumed fixing Binding would make x:Bind behave the same way), and it turns out that there're quite a lot of subtle behavioural differences between x:Bind and Binding on WinUI I'm testing on a TextBlock like this: <StackPanel>
<Button Click="Button_Click">set</Button>
<Button Click="Button_Click_1">reset</Button>
<ContentPresenter x:Name="cp">
<ContentPresenter.ContentTemplate>
<DataTemplate x:DataType="local:TestItem">
<TextBlock Text="{x:Bind Outer, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay, FallbackValue='fallback'}" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
<TextBlock Text="----------------------------------" />
<TextBlock x:Name="tb" Text="{Binding Outer, Converter={StaticResource BoolToVisibilityConverter}, FallbackValue='fallback'}" />
</StackPanel>
|
Additionally, when we set ContentPresenter.Content to null, in Uno the ContentPresenter in the xaml snipper in the previous comment will have no children, while in WinUI, the TextBlock will continue to exist as a child of the ContentPresenter (again, visually blank, so it's like it's not there). I will have to recheck this later. |
Current behavior
Converter is receiving
null
value when it should not be possible.Expected behavior
No
null
valueHow to reproduce it (as minimally and precisely as possible)
uno-bug-repro-converter-null-main.zip
Workaround
Gracefully handle
null
values in converters.Works on UWP/WinUI
Yes
Environment
No response
NuGet package version(s)
No response
Affected platforms
No response
IDE
No response
IDE version
No response
Relevant plugins
No response
Anything else we need to know?
No response
The text was updated successfully, but these errors were encountered: