Добавление элементов управления в ItemsControl в UserControl с использованием XAML

Я хочу написать элемент управления, который немного похож на CommandBar.
Используя CommandBar, вы можете написать:

<CommandBar>
    <CommandBar.PrimaryCommands>
        <AppBarButton>B1</AppBarButton>
        <AppBarButton>B2</AppBarButton>
    </CommandBar.PrimaryCommands>
</CommandBar>


У моего элемента управления также есть свойства, к которым можно добавить элементы управления в XAML.
Вот так:

<Page
    x:Class="TheApp.MainPage"
    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:local="using:App27"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <local:MyControl>
            <local:MyControl.LeftStuff>
                <Button>L1</Button>
                <Button>L2</Button>
            </local:MyControl.LeftStuff>
            <local:MyControl.MidStuff>
                <Button>M1</Button>
                <Button>M2</Button>
            </local:MyControl.MidStuff>
            <local:MyControl.RightStuff>
                <Button>R1</Button>
                <Button>R2</Button>
            </local:MyControl.RightStuff>
        </local:MyControl>
    </Grid>
</Page>


На данный момент я придумал следующий UserControl.
MyControl.xaml:

<UserControl
    x:Class="TheApp.MyControl"
    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:local="using:App27"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="300"
    d:DesignWidth="400"
    mc:Ignorable="d">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ItemsControl Grid.Column="0" ItemsSource="{x:Bind LeftStuff}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
        <ItemsControl Grid.Column="1" ItemsSource="{x:Bind MidStuff}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
        <ItemsControl Grid.Column="2" ItemsSource="{x:Bind RightStuff}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</UserControl>

MyControl.xaml.cs:

namespace TheApp
{
    using System.Collections.ObjectModel;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    public sealed partial class MyControl: UserControl
    {
        public MyControl()
        {
            this.InitializeComponent();
        }

        public static readonly DependencyProperty LeftStuffProperty =
            DependencyProperty.Register("LeftStuff", typeof(ObservableCollection<FrameworkElement>), typeof(MyControl), new PropertyMetadata(null));

        public ObservableCollection<FrameworkElement> LeftStuff
        {
            get { return (ObservableCollection<FrameworkElement>)GetValue(LeftStuffProperty); }
            set { SetValue(LeftStuffProperty, value); }
        }

        public static readonly DependencyProperty MidStuffProperty =
            DependencyProperty.Register("MidStuff", typeof(ObservableCollection<FrameworkElement>), typeof(MyControl), new PropertyMetadata(null));

        public ObservableCollection<FrameworkElement> MidStuff
        {
            get { return (ObservableCollection<FrameworkElement>)GetValue(MidStuffProperty); }
            set { SetValue(MidStuffProperty, value); }
        }

        public static readonly DependencyProperty RightStuffProperty =
            DependencyProperty.Register("RightStuff", typeof(ObservableCollection<FrameworkElement>), typeof(MyControl), new PropertyMetadata(null));

        public ObservableCollection<FrameworkElement> RightStuff
        {
            get { return (ObservableCollection<FrameworkElement>)GetValue(RightStuffProperty); }
            set { SetValue(RightStuffProperty, value); }
        }
    }
}


Это компилируется, и MainPage отображается в дизайнере, как и ожидалось. MyControl, отображаемый в дизайнере

Но когда я запускаю код, я получаю это исключение:

Исключение типа «Windows.UI.Xaml.Markup.XamlParseException» возникло в App27.exe, но не было обработано в пользовательском коде. Информация WinRT: невозможно добавить экземпляр типа «Windows.UI.Xaml.Controls.Button» в коллекцию введите «System.Collections.ObjectModel.ObservableCollection`1». [Строка: 16 Позиция: 13] ...

Так что же не так?


person O. Hahn    schedule 25.08.2016    source источник


Ответы (1)


Сам себе отвечаю на вопрос:

свойства зависимостей необходимо инициализировать, чтобы xaml мог добавлять элементы.

public static readonly DependencyProperty ...StuffProperty =
    DependencyProperty.Register(
        "...Stuff", 
        typeof(ObservableCollection<FrameworkElement>),
        typeof(MyControl),
        // The property needs to be initialized
        new PropertyMetadata(new ObservableCollection<FrameworkElement>())
    );
person O. Hahn    schedule 31.08.2016