XAML designer shows error after program has run and does not display correctly

445 views Asked by At

I have a weird error that the XAML designer displays when hovering over the text that gets the blue underline. It also refuses to show the components correctly in the preview. The text is simply

Object reference not set to an instance of an object.

That looks like a NullReferenceException, but I have no clue where it comes from. It displays correctly in the launched app.

It actually seems to be related to inheriting from List<string> AND exposing a settable property. If I remove either of that it works. But I want both for my converter.

To reproduce it, simply create an empty WPF .NET Framework project, and paste this below the MainWindow.xaml.cs code inside the namespace:

    public class BuggyConverter : List<string>, IMultiValueConverter
    {
        public object Value { get; set; }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        => Visibility.Visible;

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) => null;
    }

and this into the MainWindow.xaml:

<Window...>
    <Window.Resources>
        <local:BuggyConverter x:Key="conv" Value="{x:Static Brushes.Yellow}" />
    </Window.Resources>
    <Grid>
        <Border Visibility="{MultiBinding Converter={StaticResource conv}}" />
        
        <ItemsControl>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Visibility="{MultiBinding Converter={StaticResource conv}}" Width="100" Height="100" Background="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsSource>
                <x:Array Type="{x:Type Brush}">
                    <SolidColorBrush Color="Green" />
                    <SolidColorBrush Color="Red" />
                </x:Array>
            </ItemsControl.ItemsSource>
        </ItemsControl>
    </Grid>
</Window>

Keep MainWindow.xaml open and then launch the app via the Start button. You can see a green and a red square. Since this is hardcoded in the XAML, I'd expect the designer to show just that.

Instead, when you exit the app, the designer shows the color hexcodes instead of the colored square, seemingly because it has problems with the converter setup.

enter image description here

What's the problem?

1

There are 1 answers

4
Sunny On

The code provided by you is working fine at my end. I think this code that you posted is only meant for StackOverflow and your actual code might be different. Anyway, My educated guess for the null reference exception is that, the public Object value{get; set;} is getting null each time you try to convert something (in your actual use case scenario)

In case the code that you provided is exactly that you are using then just try to build your project to see if that null reference error goes away or else try below steps

  1. Empty your bin/debug folders from such project
  2. Restart your IDE
  3. Build your projects

enter image description here