模板ItemsControl以显示特定类型的ObservationCollection <t>

时间:2018-09-16 20:12:56

标签: c# wpf

我正在使用FormBuilder。我想在ItemsControl中显示“我的类型”的ObservationCollection并对其进行模板化:

这是我的课程:

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

这是我的风格:(我已经编辑了ItemTemplate)

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

此代码有什么问题?

0 个答案:

没有答案