Win 8 App从datatemplate listview获取文本

时间:2015-06-14 18:25:54

标签: listview text windows-8

我正在尝试学习如何在我的win 8 app中的listviews中创建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 个答案:

答案 0 :(得分:1)

我会将tapped事件设置为网格并通过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>

在你背后的代码中你可以这样做:

 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 ...
         }