简体   繁体   English

使用XAML将控件添加到UserControl中的ItemsControl

[英]Adding controls to an ItemsControl within a UserControl using XAML

I want to write an control that behaves a bit like the CommandBar. 我想编写一个行为类似于CommandBar的控件。
Using CommandBar you can write: 使用CommandBar,您可以编写:

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


My control also has properties to which controls can be added within xaml. 我的控件还具有可以在xaml中添加控件的属性。
Like this: 像这样:

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


So far I've come up with the following UserControl. 到目前为止,我已经提出了以下UserControl。
MyControl.xaml: 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: 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); }
        }
    }
}


This compiles and MainPage is rendered as expected in designer. 这将进行编译,并按设计器中的预期呈现MainPage。 在设计器中呈现的MyControl

But when I run the code I get this exception: 但是,当我运行代码时,出现以下异常:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App27.exe but was not handled in user code WinRT information: Cannot add instance of type 'Windows.UI.Xaml.Controls.Button' to a collection of type 'System.Collections.ObjectModel.ObservableCollection`1'. App27.exe中发生类型为Windows.UI.Xaml.Markup.XamlParseException的异常,但未在用户代码WinRT信息中进行处理:无法将类型为Windows.UI.Xaml.Controls.Button的实例添加到以下项的集合中类型'System.Collections.ObjectModel.ObservableCollection`1'。 [Line: 16 Position: 13] ... [行:16位置:13] ...

So what is wrong? 那怎么了?

To answer the question myself: 我自己回答问题:

the dependency properties need to be initialized so that xaml can add the elements. 需要初始化依赖项属性,以便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>())
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM