Property of ObservableCollection type generates error in designer

64 views Asked by At

I have a couple of WPF projects I am working on using Visual Studio 2019 Community. One is a control library and one is an application using the control library.

In the control library, I have a class ExteraWindow derived from a System.Windows.Window class. The relevant code for this class looks something like this:

namespace Extera.Presentation.Control
{
    public class Window : System.Windows.Window
    {
        static Window()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Window),
                new System.Windows.FrameworkPropertyMetadata(typeof(Window)));
        }

        public Window()
            : base()
        {
            // initialize the MenuItems collection
            MenuItems.CollectionChanged += Items_CollectionChanged;
        }

        public ObservableCollection<MenuItem> MenuItems { get; } = new ObservableCollection<MenuItem>();

        private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // some code here
        }

        // some more class members here
    }
}

The code for the MenuItem class looks like this:

    public class MenuItem 
    {
        public MenuItem()
        {
            Caption = "";
            Title = "";
        }

        public string Title { get; set; }
        public string Caption { get; set; }
    }

The ExteraWindow class is used in the app like this:

<control:Window x:Class="App.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:Extera.App.ProjectManager"
        xmlns:control="clr-namespace:Extera.Presentation.Control;assembly=Extera.Presentation.Control"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <control:ExteraWindow.MenuItems>
        <control:MenuItem Title="aaaa" Caption="bbbb" />
    </control:ExteraWindow.MenuItems>
    <Border BorderBrush="White" BorderThickness="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Foreground="White">a;osidjflsodjfo;sdjg</Label>
        </Grid>
    </Border>
</control:ExteraWindow>

The code compiles OK and works OK. If I set a breakpoint in the Items_CollectionChanged method of the ExteraWindow class, the breakpoint gets hit once, and that is for adding a MenuItem element with the Title "aaaa" and the Caption "bbbb", which is exactly what I expected.

But the problem is the designer for the MainWindow. Here is a screenshot: ![screenshot of Visual Studio

So, my question is: how do I clear this error? The compiler certainly does not complain about anything, so this is a designer only problem. Are there any attributes that I need to decorate my MenuItems property with? Or anything else I missed?

Thank you, Tibi.

Edit:

Noticed that the MenuItem class was posted only with a constructor, missing the properties for Title and Caption. I added them now, for completeness.

1

There are 1 answers

2
o_w On

MenuItems property does not have a 'set' in it, but you are trying to 'set' it from xaml, so maybe that is the reason.