2010-06-28 11 views
25

Vẫn lừa đảo với WPF và học khi tôi đi. Đang thử tạo một nhóm điều khiển động (chủ yếu là Nút nhưng có thể bao gồm Hộp kiểm và các hộp kiểm khác).WrapPanel dưới dạng ItemPanel cho ItemsControl

Tôi không biết cách tốt nhất để làm điều này vì vậy tôi đã thử tạo kiểu ItemsControl và sau đó thêm các mục vào một ItemsPresenter bên trong một WrapPanel. Ngay sau đó nhận ra các mục sẽ không bọc bởi vì họ có hiệu quả không phải bên trong WrapPanel trừ khi tôi đặt nó như ItemsHost. Như thế này:

<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ItemsControl}"> 
       <Border CornerRadius="5" 
         BorderBrush="{StaticResource DarkColorBrush}" 
         BorderThickness="1" 
         Margin="5"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 

         <WrapPanel IsItemsHost="True" FlowDirection="LeftToRight"> 
          <ItemsPresenter /> 
         </WrapPanel> 

         <ContentPresenter ContentSource="Name" Grid.Row="1" /> 

        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Lưu ý rằng đây là công việc đang diễn ra và có nhiều hiệu ứng tạo kiểu mà tôi vẫn cần triển khai. Ở đây tôi sử dụng nó:

<UniformGrid Rows="1"> 
    <ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button1</Button> 
     <Button>Button2</Button> 
     <CheckBox>TickBox</CheckBox> 
    </ItemsControl> 

    <ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button3</Button> 
     <Button>Button4</Button> 
     <Button>Button5</Button> 
    </ItemsControl> 

    <ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button6</Button> 
     <Button>Button7</Button> 
     <Button>Button8</Button> 
    </ItemsControl> 
</UniformGrid> 

Cũng lưu ý ở đây là nó vẫn là một công việc đang tiến như UniformGrid sẽ không phải là con đường để đi ở đây và cũng lề có thể là một nỗi đau (được có bất kỳ lợi nhuận nào trùng?) vì vậy đầu vào sẽ được đánh giá cao.

Bây giờ đến vấn đề thực sự. Điều này không có tác dụng Tôi nhận được thông báo lỗi:

'ItemsPresenter' object cannot be added to 'WrapPanel'. Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel. Error at object 'System.Windows.Controls.ItemsPresenter'.

Vì vậy, cách tốt nhất để làm điều gì đó như thế này (rất thích chỉ có thể ném nút và các điều khiển khác vào ItemControl và xếp hàng thật đẹp) . Nó sẽ là tốt hơn để đưa các điều khiển vào một bộ sưu tập của một số loại và lặp đi lặp lại.

Rất thích làm cho nó được thực hiện độc đáo nhưng kỹ năng WPF của tôi vẫn còn thiếu. Có bất kỳ cuốn sách WPF nào dạy vượt ra ngoài những điều cơ bản và cho thấy làm thế nào chuyên nghiệp sẽ thực sự làm điều đó?

Trả lời

39

Bạn có thể muốn có một cái nhìn tại ItemsPanel tài sản:

Gets or sets the template that defines the panel that controls the layout of items.

Ví dụ:

<ItemsControl> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

Và bạn có thể đặt nó trong một Style như sau:

<Style TargetType="ItemsControl"> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <WrapPanel /> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

Điều đó có tác dụng nhưng tôi không thể đặt nó theo kiểu thay vì ở mọi ItemsControl. Tôi nghĩ rằng đó là có thể với tài sản IsItemsHost nhưng dường như không hoạt động. Đây có phải là cách duy nhất không? –

+0

Câu trả lời được cập nhật với một Style cho ItemsControl – Arcturus

+0

Ah tất nhiên tôi cũng có thể thêm vào phong cách này, cảm ơn bạn. Nếu bạn có thời gian, bạn có thể xem 2 câu hỏi khác mà tôi có vẫn chưa được giải quyết. http://stackoverflow.com/questions/3118266/wpf-datatemplate-property-set-at-content http://stackoverflow.com/questions/3004967/how-to-reuse-layouts-in-wpf –

3

Đừng quên định nghĩa của tài sản đầu mối IsItemsHost = "True". Nếu không, ItemsControl của bạn sẽ không hiển thị các mục của bạn.

<ListBox ItemsSource="{Binding MyItemsSource}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate > 
       <WrapPanel IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ListBox>