【问题标题】:Property of ObservableCollection type generates error in designerObservableCollection 类型的属性在设计器中生成错误
【发布时间】:2020-04-13 01:52:46
【问题描述】:

我正在使用 Visual Studio 2019 社区处理几个 WPF 项目。一种是控制库,一种是使用控制库的应用程序。

在控件库中,我有一个从 System.Windows.Window 类派生的类 ExteraWindow。该类的相关代码如下所示:

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
    }
}

MenuItem 类的代码如下所示:

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

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

ExteraWindow 类在应用程序中的使用如下:

<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>

代码编译正常,工作正常。如果我在ExteraWindow 类的Items_CollectionChanged 方法中设置断点,则断点会被命中一次,即添加MenuItem 元素与Title“aaaa”和Caption“bbbb” ,这正是我所期望的。

但问题在于MainWindow 的设计者。这是一个屏幕截图:

所以,我的问题是:如何清除此错误?编译器当然不会抱怨任何事情,所以这是一个设计者唯一的问题。我需要用什么属性来装饰我的 MenuItems 属性吗?还是我错过了什么?

谢谢你, 蒂比。

编辑:

注意到MenuItem 类仅使用构造函数发布,缺少TitleCaption 的属性。为了完整起见,我现在添加了它们。

【问题讨论】:

    标签: c# wpf xaml listproperty


    【解决方案1】:

    MenuItems 属性中没有“设置”,但您正试图从 xaml 中“设置”它, 所以也许这就是原因。

    【讨论】:

    • 我尝试向MenuItems 添加一个集合,但没有任何改变。
    • 您是否使用“PropertyChanged”方法调用添加它?另外,我认为您没有在 Window 的构造函数中调用“InitializeComponents”方法。
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多