Шаблоны ItemsControl для отображения определенного типа ObservationCollection <T> - PullRequest
0 голосов
/ 16 сентября 2018

Я работаю над FormBuilder. Я хочу отобразить коллекцию ObservationCollection моего типа в ItemsControl и шаблонизировать ее:

Вот мой класс:

 public class Draggable : Label
 {     

     public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<Draggable>), typeof(Draggable), new PropertyMetadata(new ObservableCollection<Draggable>(), OnItemsChanged));

     public static readonly DependencyProperty HasItemProperty = DependencyProperty.Register("HasItem", typeof(bool), typeof(Draggable), new PropertyMetadata(false));

     private static void OnItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
     {
         Draggable draggable = (Draggable)obj;

         if (e.NewValue != null)
         {
             if (((ObservableCollection<Draggable>)e.NewValue).Count > 0)
                 draggable.HasItem = true;
             else
                 draggable.HasItem = false;
         }
         else
         {
             draggable.Items = new ObservableCollection<Draggable>();
             draggable.HasItem = false;
         }
     }

     public ObservableCollection<Draggable> Items
     {
         get { return (ObservableCollection<Draggable>)GetValue(ItemsProperty); }
         set { SetValue(ItemsProperty, value); }
     }


     public bool HasItem
     {
         get { return Items.Count > 0; }
         set { SetValue(HasItemProperty, value); }
     }

Вот мой стиль: (я отредактировал шаблон Item)

 <ControlTemplate TargetType="Controls:Draggable">
     <Border CornerRadius="2" MinHeight="32" MinWidth="190" BorderBrush="#CCC" BorderThickness="1">
         <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition Height="auto"/>
                 <RowDefinition Height="auto"/>
             </Grid.RowDefinitions>
             <StackPanel Margin="6 6 0 0" Orientation="Horizontal" Grid.Row="0">
                 <Border Background="Red" Width="16" Height="16"></Border>
                 <TextBlock Foreground="#333" Margin="9 0 0 0" VerticalAlignment="Center" Text="{TemplateBinding Title}"></TextBlock>
             </StackPanel>

             <!-- * **Here is the problem** * -->
             <Border x:Name="ItemsContainer" Grid.Row="1" Margin="6 20 6 6" BorderThickness="1" BorderBrush="#CCC" Background="White" Padding="6">
                    <ItemsControl ItemsSource="{Binding Items}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border BorderThickness="1" BorderBrush="#CCC" Background="White" Margin="6">
                                    <StackPanel Margin="6 6 0 0" Orientation="Horizontal" Grid.Row="0">
                                        <Border Background="Red" Width="16" Height="16"></Border>
                                        <TextBlock Foreground="#333" Margin="9 0 0 0" VerticalAlignment="Center" Text="{Binding Title}"></TextBlock>
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
             </Border>

         </Grid>
     </Border>
     <ControlTemplate.Triggers>
         <Trigger Property="HasItem" Value="False">
             <Setter TargetName="ItemsContainer" Property="Visibility" Value="Collapsed"/>
         </Trigger>
     </ControlTemplate.Triggers>
 </ControlTemplate>

У меня есть коллекция ObservationCollection, и я хочу отобразить ее в ItemsControl и связать свойства Draggable с шаблоном ItemsControl.

Этот код никогда не отображает элементы. и OnItemChanged не будет стрелять вообще.

Я использовал приведенный выше код таким образом:

        <Controls:Draggable x:Name="TestReza" Style="{StaticResource Draggie}" Title="Title" Canvas.Left="29" Canvas.Top="79">
            <Controls:Draggable.Items>
                <Controls:Draggable Title="Reza"></Controls:Draggable>
            </Controls:Draggable.Items>
        </Controls:Draggable>

Что не так с этим кодом?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...