如何知道WP7中Grid中单击了哪一行(并访问其数据)?

时间:2012-09-12 16:57:20

标签: c# wpf silverlight windows-phone-7

我有以下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中没有这样的东西。非常感谢你。

1 个答案:

答案 0 :(得分: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;}
}