2012-08-17 66 views
0

對於C#/ XAML中的Windows 8應用程序,我希望能夠知道標題模板中按鈕的位置(X和Y)訪問分組網格中的標題模板的元素

下面是一個簡單的XAML代碼:一個分組網格視圖

<GridView x:Name="PicturesGridView" SelectionMode="None"  
ItemsSource="{Binding Source={StaticResource cvs1}}" IsItemClickEnabled="True" 
ItemTemplate="{StaticResource CustomTileItem}" ItemClick="ItemView_ItemClick" IsSwipeEnabled="True"> 

      <GridView.ItemsPanel> 
      <ItemsPanelTemplate> 
    <VirtualizingStackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </local:MyGridView.ItemsPanel> 
     <local:MyGridView.GroupStyle> 
      <GroupStyle x:Name="MyGroupStyle"> 
       <GroupStyle.HeaderTemplate > 
        <DataTemplate x:Name="MyDataTemplate"> 
         <Button x:Name="HeaderButton" Click="Button_Click_1" Content="{Binding Key}" Foreground="Black" Background="White" FontSize="30" Margin="0,0,0,-10" ></Button> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
       <GroupStyle.Panel> 
        <ItemsPanelTemplate> 
    <VariableSizedWrapGrid ItemWidth="75" ItemHeight="150" Orientation="Vertical" Margin="0,0,80,0" MaximumRowsOrColumns="3"/> 
        </ItemsPanelTemplate> 
       </GroupStyle.Panel> 
      </GroupStyle> 
     <GridView.GroupStyle> 
    <GridView> 

我在做這個訪問的按鈕頭模板中取得成功:

  var template = element.FindName("PicturesGridView") as MyGridView; 
      var group = template.GroupStyle[0] as GroupStyle; 
      var buttonHeader = group.HeaderTemplate.LoadContent() as Button; 

但後來我可以不區分我的模板的每個按鈕。我想要一個代表我的數據和他們的位置的物理按鈕陣列。

謝謝您的幫助

回答

1

的WinRT的無線工具包有你所需要的...退房http://winrtxamltoolkit.codeplex.com/downloads/get/467926,並期待在VisualTreeHelperExtensions。

使用擴展方法GetDescendantsOfType可以寫像以下代碼:

VAR鍵= PicturesGridView.GetDescendantsOfType()ToArray的();

現在,這將爲您提供PicturesGridView中的所有按鈕,因此如果您的項目模板也包含按鈕,您也將獲得這些按鈕。您可以在標題模板中的按鈕上設置Tag屬性,以便您可以從其他按鈕輕鬆識別它們。