【问题标题】:Initialize custom collection without array in XAML在 XAML 中初始化没有数组的自定义集合
【发布时间】:2018-03-22 17:00:08
【问题描述】:

我正在创建一个自定义控件,其中包含要显示的对象集合。我希望这是一个包含一些东西的包装器和一个内部部分,它承载由控件的使用者确定的其他东西。基本上,我希望它的使用与 StackPanel 类似,用户在其中创建它并在内容部分添加控件。我基本上有一个我无法弄清楚的事情。这是需要设置的“页面”依赖属性:

    public static readonly DependencyProperty PagesProperty =
        DependencyProperty.Register("Pages", typeof(IEnumerable<MyContentPage>), typeof(UC_ApplicationWindow),
            new PropertyMetadata(new List<MyContentPage>()));

    public IList<MyContentPage> Pages
    {
        get => (IList<MyContentPage>)GetValue(PagesProperty);
        set => SetValue(PagesProperty, value);
    }

这是当前正在运行的控件的消费者窗口中的代码:

        <graphicElements:UC_ApplicationWindow.Pages>
            <x:Array Type="graphicElements:MyContentPage">
                <graphicElements:MyContentPage>
                    <graphicElements:MyContentPage.Content>
                        ...the contents of the page
                    </graphicElements:MyContentPage.Content>
                </graphicElements:MyContentPage>
                <graphicElements:MyContentPage>
                    <graphicElements:MyContentPage.Content>
                        ...the contents of the page
                    </graphicElements:MyContentPage.Content>
                </graphicElements:MyContentPage>
            </x:Array>
        </graphicElements:UC_ApplicationWindow.Pages>

理想情况下,我希望能够为 Pages 属性设置多个元素,而不必给它一个数组,但是如果我尝试直接这样做,我会收到错误“无法分配指定的值。预期以下类型:“IList'1”。我也希望能够像大多数其他 WPF 控件一样直接指定页面内容,但是当我取出直接内容属性时,它会显示“'MyContentPage' 类型不支持直接内容”。这是我最终希望它能够看起来的样子:

        <graphicElements:UC_ApplicationWindow.Pages>
            <graphicElements:MyContentPage>
                ...the contents of the page
            </graphicElements:MyContentPage>
            <graphicElements:MyContentPage>
                ...the contents of the page
            </graphicElements:MyContentPage>
        </graphicElements:UC_ApplicationWindow.Pages>

正如我所说,它目前有效,如果这是我必须做的,我可以,但它似乎有点笨拙,我有预感它可以更好地工作,但无法弄清楚如何。有人有我丢失的魔法钥匙吗?

【问题讨论】:

    标签: wpf xaml user-controls dependency-properties


    【解决方案1】:

    好的,感谢this post,我想通了。您需要做的就是将“ContentProperty”属性添加到顶部,当直接指定时,XAML 会将该属性解析为内容。所以在这种情况下,我实际上做了两次。我将 [ContentProperty(nameof(Pages))] 放在主应用程序窗口类上,将 [ContentProperty(nameof(Content))] 放在 MyContentPage 类上。

    感谢this post 上的评论现在代替这个:

    <UC_ApplicationWindow>
        <graphicElements:UC_ApplicationWindow.Pages>
            <x:Array Type="graphicElements:MyContentPage">
                <graphicElements:MyContentPage>
                    <graphicElements:MyContentPage.Content>
                        ...the contents of the page
                    </graphicElements:MyContentPage.Content>
                </graphicElements:MyContentPage>
                <graphicElements:MyContentPage>
                    <graphicElements:MyContentPage.Content>
                        ...the contents of the page
                    </graphicElements:MyContentPage.Content>
                </graphicElements:MyContentPage>
            </x:Array>
        </graphicElements:UC_ApplicationWindow.Pages>
    </UC_ApplicationWindow>
    

    我可以这样做:

    <UC_ApplicationWindow>
        <graphicElements:MyContentPage>
            ...the contents of the page
        </graphicElements:MyContentPage>
        <graphicElements:MyContentPage>
            ...the contents of the page
        </graphicElements:MyContentPage>
    </UC_ApplicationWindow>
    

    啊……干净多了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2014-09-13
      • 2018-10-05
      相关资源
      最近更新 更多