1

I am having a problem with the Visual Studio designer for a WPF project and the combination of binding to a type using a generic and specifying a nullable type as the generic type.

I have tried to construct a minimal example of the problem:

XAML:

<Window x:Class="TestWpfApp.MainWindow"
        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"
        xmlns:local="clr-namespace:TestWpfApp" 
        mc:Ignorable="d" d:DataContext="{d:DesignInstance local:TestViewModel}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Text="{Binding TestText.Value}"/>
        <!--<TextBlock Text="{Binding TestTextValue}"/>-->
    </Grid>
</Window>

Code behind:

using System.Windows;

namespace TestWpfApp
{
    public class TestGeneric<T>
    {
        public TestGeneric(T value)
        {
            Value = value;
        }

        public T Value { get; }
    }

    public class TestViewModel
    {
        public TestGeneric<double?> TestText { get; } = new TestGeneric<double?>(123.456);
        public double? TestTextValue => TestText.Value;
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new TestViewModel();
            InitializeComponent();
        }
    }
}

The designer fails with this code with the following error message: System.Runtime.Remoting.RemotingException [16040] Designer process terminated unexpectedly!

The commented out line in the XAML code does not give the error in the designer window.

Both versions actually work when running the project. It is only the designer that fails.

Does anyone have any idea about what the problem could be?

1 Answers1

0

The d:DataContext design time expression is a very practical trick, and IDE related, it has no impact on the runtime, only affect the design time. Applicable in Visual Studio 2010 and later.

The default constructor is required for a type to be instantiated in XAML.

The option IsDesignTimeCreatable=True tells the designer that it can create the specified class via default constructor. This way it is possible to provide sample data for the UI.

d:DataContext="{d:DesignInstance   local:TestViewModel , IsDesignTimeCreatable=True  }"

Yong Lu

Yohann
  • 224
  • 1
  • 7
  • Hi Yong Lu. The problem here is purely design time. The application works in both cases at run-time. The option IsDesignTimeCreatable=True does not fix the problem with the unhandled exception in the Visual Studio Designer. – Dennis Pedersen Sep 18 '19 at 09:03
  • I have made a test on my side, It is working well. The 'Designer process terminated unexpectedly!' will disappear when I use the option **IsDesignTimeCreatable=True**. I use the Visual Studio 2019 Version 16.2.3 – Yohann Sep 19 '19 at 01:27
  • Hi Yong Lu. I just tested on my side with Visual Studio Professional 2019. The designer still fails but with a System.ArgumentNullException now.. Seems like the problem is not related to the IsDesignTimeCreatable attribute but something else. – Dennis Pedersen Sep 24 '19 at 13:25
  • You may can go to [the Developer Community](https://developercommunity.visualstudio.com/spaces/8/index.html) Visual Studio and report this issue. – Yohann Sep 25 '19 at 05:28