2015-06-14 179 views
0

我想學習如何在列表視圖的DataTemplate在我贏8應用文本贏8應用程序獲取從列表視圖的DataTemplate

我有下面的代碼在我的XAML代碼

<!-- Vertical scrolling item list --> 
    <ListView x:Name="itemListView" 
     Margin="0,4.714,10,0.429" 
     ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
     SelectionChanged="itemListView_SelectionChanged" Loaded="itemListView_Loaded" Grid.Row="4" Tapped="itemListView_Tapped_1"> 

     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Grid Height="110" Margin="6"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110"> 
         <Image Source="{Binding Image}" Stretch="UniformToFill"/> 
        </Border> 
        <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0"> 
         <TextBlock Text="{Binding Title}" TextWrapping="NoWrap" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Subtitle}" TextWrapping="NoWrap"/> 
         <TextBlock Text="{Binding Description}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description2}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description3}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description4}" MaxHeight="60" FontFamily="Global User Interface"/> 
        </StackPanel> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

這使6列表視圖上每個項目的文本塊

問題我有當我在列表視圖中選擇一個項目我使用輕擊事件然後填充詳細信息窗格,但我無法解決如何從選定的項目中獲取6個項目

有人點我在正確的方向,請

任何幫助表示讚賞

馬克

回答

1

我會抽頭事件設置爲電網和經過XAML路徑。事情是這樣的:

XAML:

<ListView x:Name="itemListView" 
    Margin="0,4.714,10,0.429" 
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
    SelectionChanged="itemListView_SelectionChanged" Loaded="itemListView_Loaded" Grid.Row="4" > 

     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Grid Height="110" Margin="6" Tapped="listViewItem_Tapped">      
        <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110"> 
         <Image Source="{Binding Image}" Stretch="UniformToFill"/> 
        </Border> 
        <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0"> 
         <TextBlock Text="{Binding Title}" TextWrapping="NoWrap" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Subtitle}" TextWrapping="NoWrap"/> 
         <TextBlock Text="{Binding Description}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description2}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description3}" MaxHeight="60" FontFamily="Global User Interface"/> 
         <TextBlock Text="{Binding Description4}" MaxHeight="60" FontFamily="Global User Interface"/> 
        </StackPanel> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

和INT後面的代碼,你可以這樣做:

private async void listViewItem_Tapped(object sender, TappedRoutedEventArgs e) 
     {   
       Grid gridElement = (sender as Grid); 
       //Get the stackpanel 
       Stackpanel stackPanelWithElements = gridElement.Children.ElementAt(1) as Stackpanel; 
       //get the first TextBlock  
       TextBlock titleTextBlock = stackPanelWithElements.Children.ElementAt(0) as TextBlock; 

       //Get the text of the textblock 
       String title = titleTextBlock.Text; 

       //Get the next text 
      TextBlock subTitleTextBlock = stackPanelWithElements.Children.ElementAt(1) as TextBlock; 
      String subTitle = subTitleTextBlock.Text; 

      //get the other elements ... 
     } 
+0

感謝這個XAML進去不錯,但我得到了竊聽事件的錯誤說「的類型或Namplace名稱」StackPanel「找不到」 –

+0

排序它Stackpanel被拼寫錯誤很多謝謝 –