1

我有以下 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或类似的东西,但似乎网格中没有类似的东西。非常感谢你。

4

1 回答 1

1

将您的数据放入一个按钮,并订阅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;}
}
于 2012-09-12T17:26:43.550 回答