2013-06-12 73 views
0

我是wpf的新手,有我希望的一个简单问题。WPF访问网格项onClick

我创建了一个简单的模板来显示一些分组的项目

目前,当我点击的项目ItemsClick被称为之一(如预期)

private void itemGridView_ItemClick(object sender, ItemClickEventArgs e) 
    { 

     MyCustomClass selectedItem = (MyCustomClass)e.ClickedItem; 
     GridView g = (GridView)sender; 

我可以得到类的副本对象通过投射e.ClickedItem和来自发件人的网格。但是我没有看到的是如何获得对模板中添加的自定义项的引用,例如,如果我想更改testName TextBlock中的文本?

模板:

<DataTemplate x:Key="MyTestTemplate"> 
      <Grid HorizontalAlignment="Left" > 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
       <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Grid.RowSpan="3"> 
        <Image Source="{Binding ThumbnailUrl}" Stretch="Fill" Width="175" Height="175" /> 
       </Border> 
       <Image Source="{Binding MyCustomImage}" Height="30" Width="30" Grid.RowSpan="3" Margin="0,0,0,30"/> 
       <Grid x:Name="ItemDetails" VerticalAlignment="Bottom" Height="75" Margin="0,0,0,2" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="40"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <TextBlock x:Name="testName" Text="{Binding Name}" Grid.Row="0" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" MaxWidth="100" MaxHeight="80" TextWrapping="Wrap" HorizontalAlignment="Left" Style="{StaticResource TitleTextStyle}" Margin="5,0,5,0"/> 
        <TextBlock Text="{Binding Address}" Grid.Row="0" HorizontalAlignment="Right" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource 

CaptionTextStyle}" TextWrapping="NoWrap" Margin="5,0,5,0" /> 
        </Grid> 
       </Grid> 
      </DataTemplate> 


<GridView 
      x:Name="itemGridView" 
      AutomationProperties.AutomationId="ItemGridView" 
      AutomationProperties.Name="Grouped Items" 
      Grid.RowSpan="2" 
      Padding="116,137,40,46" 
      ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}" 
      ItemTemplate="{StaticResource MyTestTemplate}" 
      SelectionMode="None" 
      IsSwipeEnabled="True" 
      IsItemClickEnabled="True" 
      RightTapped="itemGridView_RightTapped" 
      ItemClick="itemGridView_ItemClick" SelectionChanged="itemGridView_SelectionChanged"> 
      <GridView.ItemsPanel> 
       <ItemsPanelTemplate>       
        <VirtualizingStackPanel Orientation="Horizontal"/> 
       </ItemsPanelTemplate> 
      </GridView.ItemsPanel> 
      <GridView.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.HeaderTemplate> 
         <DataTemplate> 
          <Grid Margin="1,0,0,6"> 
           <Button 
            AutomationProperties.Name="Group Title" 
           > 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding state}"/> 
            </StackPanel> 
           </Button> 
          </Grid> 
         </DataTemplate> 
        </GroupStyle.HeaderTemplate> 
        <GroupStyle.Panel> 
         <ItemsPanelTemplate> 
          <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/> 
         </ItemsPanelTemplate> 
        </GroupStyle.Panel> 
       </GroupStyle> 
      </GridView.GroupStyle> 
     </GridView> 
+0

您的代码显示项目绑定,但不显示集合的绑定。你也可以包括吗? –

回答

0

好吧,我发现MSDN上的答案。

这里http://msdn.microsoft.com/en-us/library/bb613579.aspx

的基本步骤

链接获得所选择的项目(我是在回应点击所以这个信息很容易),发现项目内的ContentPresenter,然后调用FindName上的DataTemplate在该ContentPresenter上设置。