2011-11-23 43 views
4

我寫這個孩子的遊戲(內存),並有一個列表瓷磚(列表),我綁定到一個項目控制在一個wrappanel內。現在我有22塊瓷磚,它們排列在中間的兩排。使用uniformgrid排列方形按鈕

我真正想要的是將它安排在屏幕中心的5x5矩陣中,因此它隨瓷磚的大小而變化。當使用均勻的網格時,我無法讓拼貼顯示正確,尺寸非常小,並且位於屏幕的左上角。當我設置列和行屬性時,它顯示出來,就好像它在界外。任何人都可以幫忙

XAML:

<Window x:Class="MemoryWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="Button" x:Key="TransparentButton"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border Background="Transparent"> 
          <ContentPresenter/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <UniformGrid Columns="5" Rows="5"> 
     <UniformGrid.Background> 
      <ImageBrush x:Name="backBrush"/> 
     </UniformGrid.Background>   
     <ItemsControl ItemsSource="{Binding Tiles}" VerticalAlignment="Center" HorizontalAlignment="center" Margin="100"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Style="{StaticResource TransparentButton}" BorderThickness="0" Padding="-4" Command="{Binding TurnTileCommand}" Opacity="{Binding OpacityVal}" Margin="10"> 
         <Image Width="150" Height="150" Source="{Binding ImageUri}"/> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
     <TextBlock Text="{Binding AmountTilesLeft}" VerticalAlignment="Bottom" FontSize="15"/> 
    </UniformGrid> 
</Window> 

回答

17

你把ItemsControlUniformGrid(這就是爲什麼控制是如此之小),但統一的電網應該是ItemsControlItemsPanel(這是currrently一個WrapPanel)。

<ItemsControl ItemsSource="{Binding Tiles}" VerticalAlignment="Center" HorizontalAlignment="center" Margin="100"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Button Style="{StaticResource TransparentButton}" BorderThickness="0" Padding="-4" Command="{Binding TurnTileCommand}" Opacity="{Binding OpacityVal}" Margin="10"> 
        <Image Width="150" Height="150" Source="{Binding ImageUri}"/> 
       </Button> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="5" Rows="5"> 
        <UniformGrid.Background> 
         <ImageBrush x:Name="backBrush"/> 
        </UniformGrid.Background> 
       </UniformGrid> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
+0

再次感謝:-)它完美地運作 –

+0

@ H4mm3rHead:不客氣:) –