如何知道在WP7的网格中单击了哪一行(并访问其数据)



我有以下XAML:

<Grid x:Name="ContentPanelDaily"
  Grid.Row="1"
  <Grid.RowDefinitions>
    <RowDefinition Height="40" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
     <ColumnDefinition Width="60" />
     <ColumnDefinition Width="80" />
  </Grid.ColumnDefinitions>
  <TextBlock Grid.Row="0"
     Grid.Column="0"
     Style="{StaticResource PhoneTextAccentStyle}"
     Margin="0,0,0,0">
            First
   </TextBlock>
   <TextBlock Grid.Row="0"
     Grid.Column="1"
     Style="{StaticResource PhoneTextAccentStyle}"
     Margin="0,0,0,0">
          Second
   </TextBlock>
</Grid>

如何检测Tap事件中单击了哪一行?我试图找到SelectedRow或类似的东西,但在Grid中似乎没有类似的东西。非常感谢。

将您的数据放入一个按钮中,并订阅on OnClick事件。

<Button OnClick="evetnHandler">
<TextBlock Grid.Row="0"
     Grid.Column="0"
     Style="{StaticResource PhoneTextAccentStyle}"
     Margin="0,0,0,0">
            First
   </TextBlock>
</Button>

此外,正如@Rachel所说:你可以使用ListBox。

<ListBox ItemsSource="{Binding Items}" 
  SelectedItem="{Binding Selected, Type=TwoWay}">
<ListBox.ItemTemplate>
<DataTemlate>
    <TextBlock
         Style="{StaticResource PhoneTextAccentStyle}"
         Margin="0,0,0,0" Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

并在代码背后创建

public ObservableCollection<DataItem> Items {get;set;}
private DataItem _selected;
public DataItem Selected
{
  get {return _selected;}
  set 
{
  _selected = value;
  //ha! item selected!!! handle it
}
}
public class DataItem 
{
   public string Name {get;set;}
}

最新更新