2012-06-14 21 views
1

我嘗試在WPF中實現Whac a mole gamePutCheckBoxes或圖像到UniformGrid中

Molehills在 UniformGrid。取決於選項molehills是複選框或圖像。麻煩來了。

我不知道如何實現 UniformGrid同時接受複選框圖像0​​。
也許你最好知道我該如何解決它。也許 UniformGrid複選框和圖像不是好主意。

我試着這樣說:

<ItemsControl Name="GameBlocksItemsControl" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Moles}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Conf.Cols}" 
          Rows="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Conf.Rows}" 
          Name="MolesGrid"> 
      </UniformGrid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

對象 S的集合,這樣我就可以把有複選框的BitmapImage

不幸的是增加收集一些的BitmapImage年代後則不能正確顯示(而不是像那裏寫的是: System.Windows.Media.Imaging.BitmapImage)。如果我將複選框 es添加到集合中,它們顯示的很好。

我考慮寫轉換,但這種轉換器將必須知道,如果指定對象複選框的BitmapImage。即使它是可能的代碼,它不是很優雅的解決方案

回答

0

個人而言,我會簡單地設置基礎上,SelectedViewOption

例如,ItemTemplate,假設SelectedViewOption是爲了簡單的字符串:

<Style x:Key="MyItemsControlStyle" TargetType="{x:Type ItemsControl}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding SelectedViewOption}" Value="Image"> 
      <Setter Property="ItemTemplate" Value="{StaticResource ImageTemplate}" /> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding SelectedViewOption}" Value="CheckBox"> 
      <Setter Property="ItemTemplate" Value="{StaticResource CheckBoxTemplate}" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

這您不僅可以在以後輕鬆添加其他查看選項,還可以從DataContext中刪除任何特定於查看的代碼,例如CheckBoxesImages。通常,您希望將用戶界面和業務邏輯層分開,並允許您這樣做。