0

我有这个问题,我正在尝试创建一个内部具有相同控件的控件。

我的控制代码在这里:

<UserControl x:Class="SubEventsControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:currentControl="clr-namespace:MyNamespace"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="400"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             mc:Ignorable="d">

    <Grid x:Name="LayoutRoot" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ItemsControl x:Name="SubEventsItemsControl"
                      Grid.Row="0"
                      Margin="5,0,5,5"
                      ItemsSource="{Binding ControlSubEvents}"
                      ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <ItemsPresenter />
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <!--  Header  -->
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <!--  Sub event text  -->
                            <TextBlock Grid.Column="0"
                                       Margin="3"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center"
                                       Style="{StaticResource DynamicTextBlockStyle}"
                                       Text="{Binding Path=SubEventName}"
                                       TextWrapping="Wrap" />


                            <Border Grid.Column="1"
                                    Width="10"
                                    Height="10"
                                    HorizontalAlignment="Right"
                                    Background="{Binding RegistrationStatusId,
                                                         Converter={StaticResource BackgroundConverter}}"
                                    CornerRadius="5"
                                    ToolTipService.ToolTip="{Binding RegistrationStatusLabel}" />
                        </Grid>

                        <!--  Subevent's subevents  -->
                        <currentControl:SubEventsControl Grid.Row="1" ControlSubEvents="{Binding Path=SubEvents}" />

                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

而这背后的类是这样的:

namespace MyNamespace
{
    public partial class SubEventsControl : UserControl, INotifyPropertyChanged
    {

        private PortalDomainContext _portalDomainContext;
        private EntityCollection<SubEvent> _controlSubEvents;

        public SubEventsControl()
        {
            InitializeComponent();
        }



        public event PropertyChangedEventHandler PropertyChanged;

        public PortalDomainContext PortalDomainContext
        {
            get
            {
                return _portalDomainContext;
            }

            set
            {
                if (_portalDomainContext != value)
                {
                    _portalDomainContext = value;
                    this.NotifyChange("PortalDomainContext");
                }
            }
        }

        public EntityCollection<SubEvent> ControlSubEvents
        {
            get { return _controlSubEvents; }
            set
            {
                if (_controlSubEvents != value)
                {
                    _controlSubEvents = value;
                    NotifyChange("ControlSubEvents");
                }
            }
        }


        // Dependency properties declaration
        public static readonly DependencyProperty SubEventsProperty = DependencyProperty.Register(
            "ControlSubEvents",
            typeof(EntityCollection<SubEvent>),
            typeof(SubEventsControl),
            new PropertyMetadata(null, OnTestEventsChanged));

        private static void OnTestEventsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {   
        }


        protected void NotifyChange(params string[] properties)
        {
            if (PropertyChanged != null)
            {
                foreach (string property in properties)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }
        }
    }
}

我在另一个控件上使用它,我得到了第一级,但不是第二级,我做错了什么?我错过了一些绑定问题吗?我想我应该是可能的,但也许我错了?

4

1 回答 1

1

您不能以声明方式在其自身中嵌套控件实例,因为这会导致无限递归。

最好通过代码将控件的实例添加到与子元素相同的控件的实例中。

于 2012-05-25T15:31:17.323 回答