2013-11-28 71 views
1

我有一個IList的字符串,我想按照某種向下和左側的網格順序排列每個按鈕;WPF UniformGrid以相反的順序

enter image description here

但是我不能想辦法做到這一點,作爲字符串的數量不是有限的,所以我不能只使用與COL /行定義一個網格。有沒有人試圖在WPF中做到這一點?

+1

'UniformGrid'不會做這。你需要一個常規的'Grid'並定義每個項目的'Grid.Row'和'Grid.Column'附屬屬性,也可以看看[Rachel的Grid Helper](http://rachel53461.wordpress.com/2011/09/) 17/wpf-grids-rowcolumn-count-properties /) –

+0

該死的,這很煩人。我會研究一下Grid幫助器。謝謝。 –

回答

1

你可以用WrapPanel來做到這一點。 WrapPanel通常不支持從右到左,但你可以通過翻轉佈局變換整個面板,然後翻轉再次爲每個孩子避開它:

<Window.Resources > 
    <x:Array Type="{x:Type sys:String}" x:Key="theItems" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
     <sys:String>1</sys:String> 
     <sys:String>2</sys:String> 
     <sys:String>3</sys:String> 
     <sys:String>4</sys:String> 
     <sys:String>5</sys:String> 
     <sys:String>6</sys:String> 
     <sys:String>7</sys:String> 
    </x:Array> 
</Window.Resources> 

<Grid> 
    <ItemsControl Name="itemsControl" FontSize="72" ItemsSource="{StaticResource theItems}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Vertical" HorizontalAlignment="Right"> 
        <WrapPanel.LayoutTransform> 
         <ScaleTransform ScaleX="-1" ScaleY="1" /> 
        </WrapPanel.LayoutTransform> 
       </WrapPanel> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemContainerStyle> 
      <Style TargetType="ContentPresenter"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Button Width="100" Height="100" Margin="10"> 
           <ContentPresenter Content="{Binding}"/> 
          </Button> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="LayoutTransform"> 
        <Setter.Value> 
         <ScaleTransform ScaleX="-1" ScaleY="1" /> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
    </ItemsControl> 
+0

我其實也是這樣做的。哈哈。標記爲答案。 –