2012-05-25 43 views
0

我有這個問題,我試圖創造條件,有其自身內同一控制的控制。Silverlight控件沒有顯示

我的控制代碼是在這裏:

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

我用它在另一個控制和我得到的第一個層次,而不是第二級別,我做錯了什麼?我是否錯過了一些綁定問題?我認爲我應該有可能,但也許我錯了?

回答

1

您不能嵌套一個控件的實例本身聲明因爲這將導致無限遞歸。

通過代碼將您的控件的實例添加到與子元素相同的控件的實例中。

+0

我應該使用什麼事件?我沒有看到方法,我可以使用,也許我也從錯誤的地方看? ItemsControl控件是否有一些方法/事件來加載它?或者我應該使用哪個事件? – arjasepp

+0

好吧,我想我只會動態地生成整個表單。我必須弄清楚如何去做,謝謝你的回答。如果有人有一個好的答案,他們可以在這裏回答。 – arjasepp

+0

爲什麼不在模板中使用其他類型的控件?你需要兩個以上的深度級別嗎? –