2014-03-05 26 views
2

我需要如下的窗口;具有多個項目的統一網格控件C#WPF

enter image description here

我試圖做到這一點,但不能得到100%的結果。我在哪裏做錯了?

<UniformGrid> 
    <ItemsControl ItemsSource="{Binding Data}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border BorderBrush="Black" Background="Gainsboro" BorderThickness="3" Margin="2" Width="100" > 
       <Grid FlowDirection="LeftToRight"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition/> 
       </Grid.ColumnDefinitions> 

       <Label HorizontalAlignment="Center" Content="{Binding Label1Text}" Grid.Row="1" Margin="2"/> 

       <Label HorizontalAlignment="Center" Content="{Binding Label2Text}" Grid.Row="2" Margin="2"/> 

       <Button HorizontalAlignment="Center" Content="Button1" Width="80" Height="80" 
         Command="{Binding DataContext.Command1, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
         CommandParameter="{Binding}" 
         Grid.Row="0" Margin="2"/> 
      </Grid> 
     </Border> 

     </DataTemplate> 
     </ItemsControl.ItemTemplate> 

     <ItemsControl.Template> 
      <ControlTemplate TargetType="ItemsControl"> 
       <ScrollViewer CanContentScroll="True"> 
        <ItemsPresenter/> 
       </ScrollViewer> 
      </ControlTemplate> 
    </ItemsControl.Template> 

    </ItemsControl> 
    </UniformGrid> 

結果窗口;

enter image description here

回答

2

嘗試改變ItemsPanel模板UniformGrid,而不是包裹內UniformGridItemsControl。您也可以嘗試使用WrapPanel代替UniformGrid

....... 
<ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
       <UniformGrid Columns="2"/> 
       <!-- <WrapPanel/> --> 
     </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 
....... 
+0

我得不到你,我必須改變「」到「」? – iJay

+0

我的意思是,除了''你已經擁有 – har07

+1

謝謝@ har07:D Works Fine !!!!!!試着設置上面的'' – iJay

0

你需要指定的列數:

<UniformGrid Columns="2"> 
.... 
相關問題