Search code examples
c#.netwpf

DataTemplateSelector that's using boolean value as an object reports an issue in XAML Designer


DISCLAIMER

This issue is only reported in the XAML Designer itself, when I run the application in debug mode, I can't see any XAML Binding issues at a runtime, and the ContentPresenter works as expected.

DataTemplateSelector:

internal sealed class BooleanDataTemplateSelector : DataTemplateSelector
{
    #region Public Properties

    public DataTemplate? FalseDataTemplate { get; set; }
    public DataTemplate? TrueDataTemplate { get; set; }

    #endregion Public Properties

    #region Public Methods

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (FalseDataTemplate == null || TrueDataTemplate == null) return new DataTemplate();

        var isLoading = (bool)item;
        return isLoading ? TrueDataTemplate : FalseDataTemplate;
    }

    #endregion Public Methods
}

ResourceDictionary which holds the template and selector with x:Key:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mat="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:pages="clr-namespace:ComatiQ.Configurator.Client.Core.ViewModels.Pages;assembly=ComatiQ.Configurator.Client.Core"
    xmlns:resx="clr-namespace:ComatiQ.Configurator.Client.Wpf.Strings.Pages.HomePage"
    xmlns:selectors="clr-namespace:ComatiQ.Configurator.Client.Wpf.Selectors">
    <DataTemplate
        x:Key="ContentLoadingTemplate"
        DataType="{x:Type pages:HomePageViewModel}">
        <mat:Card>
            <StackPanel
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Orientation="Vertical">
                <TextBlock
                    Margin="0,0,0,10"
                    Style="{StaticResource MaterialDesignHeadline6TextBlock}"
                    Text="{x:Static resx:HomePage.RouteOperation_Loading}" />
                <ProgressBar
                    Height="20"
                    IsIndeterminate="True"
                    Style="{StaticResource MaterialDesignLinearProgressBar}" />
            </StackPanel>
        </mat:Card>
    </DataTemplate>

    <DataTemplate
        x:Key="NullTemplate" />

    <selectors:BooleanDataTemplateSelector
        x:Key="LoadingTemplateSelector"
        FalseDataTemplate="{StaticResource NullTemplate}"
        TrueDataTemplate="{StaticResource ContentLoadingTemplate}" />
</ResourceDictionary>

HomePageView.xaml part of the code which reports an issue:

    <Grid
        Grid.Row="1"
        Grid.Column="1"
        Margin="2.5"
        Panel.ZIndex="1">
        <!--  CONTENT VIEWER  -->
        <ContentPresenter
            Panel.ZIndex="0"
            Content="{Binding DisplayedViewModel}"
            ContentTemplateSelector="{StaticResource ViewContentTemplateSelector}" />

        <!--  CONTENT LOADING INFO  -->
        <ContentPresenter
            Panel.ZIndex="1"
            Content="{Binding IsRouteLoading}"
            ContentTemplateSelector="{StaticResource LoadingTemplateSelector}" />
    </Grid>

The ContentPresenter under the reports:

**Severity  Code    Description Project File    Line    Suppression State
Error   XDG0066 Object reference not set to an instance of an object.   ComatiQ.Configurator.Client.Wpf D:\Programming\Projects\DomatiQ DALI Configurator\DomatiQ DALI Configurator\ComatiQ.Configurator.Client.Wpf\Views\Pages\HomePageView.xaml   138 
**

I created a lot of software solutions for my company, which are WPF based, yet I never encountered this particular issue. Is this a Visual Studio 2022 XAML Designer Bug, because otherwise I've no idea why I get any errors.


Solution

  • Turns out it is a Visual Studio 2022 XAML Designer bug.

    I'm now using Rider which I prefer anyway, and use VS2022 just to apply formatting when creating PR's.