无法获取网格或列表项中单击项的索引

时间:2011-01-11 05:20:32

标签: c# silverlight visual-studio-2010 windows-phone-7 listbox

我正在使用Windows 2010手机版的VS 2010 Express版本工作。我有使用wrappanel制作的图像网格。当我选择任何项目或图像时,我想要点击图像的ID。同样有一个列表框和任何列表项单击时,我再次想要单击文本块的值,即文本。 我正在使用此方法进行列表框项目选择,想要这里的id:

private void on_selection(object sender, SelectionChangedEventArgs e)
{ 
   //want id of clicked item here
}

void grid_image_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
   //want id of clicked image in grid here
}

欢迎任何建议。

列表框的xml是:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="768"/>
        <RowDefinition Height="0*" />
    </Grid.RowDefinitions>


    <ListBox Name="Index_list" SelectionChanged="on_selection">

    </ListBox> 
    <Image Visibility="Collapsed" Margin="0,151,0,200" Name="selected_image"></Image>
</Grid>

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用事件参数SelectionChangedEventArgse.AddedItems属性。

正如文档所说,这会给你

  

自上次发生SelectionChanged事件以来所选择的项目。

具体来说,它将为您提供所选绑定对象的IList。这并没有给你索引,但是一旦你拥有了所选的项目,它的prettys很容易获得索引,如果这是你真正想要的(例如IndexOf你已经绑定的那些)。

您还可以将发件人转换为列表框,然后检查SelectedIndex,但这对列表框有问题。

答案 1 :(得分:1)

假设以下XAML:

<ListBox Name="Index_list" SelectionChanged="on_selection">
    <!-- These items could also be added in code -->
    <TextBlock Text="list box option 1" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 2" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 3" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 4" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 5" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 6" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 7" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 8" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 9" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 10" Style="{StaticResource PhoneTextExtraLargeStyle}" />
    <TextBlock Text="list box option 11" Style="{StaticResource PhoneTextExtraLargeStyle}" />
</ListBox>

您可以通过以下方式获取所选TextBlock的文本 (请注意,使用MessageBox纯粹是为了演示。)

private void on_selection(object sender, SelectionChangedEventArgs e)
{
    // As the listbox is named we can do this:
    if (Index_list.SelectedIndex >= 0)
    {
        MessageBox.Show((Index_list.SelectedItem as TextBlock).Text);
    }

    // if the listbox wasn't named we could do this:
    if (sender is ListBox) // always good to double check
    {
        var sal = sender as ListBox;

        if (sal.SelectedIndex >= 0)
        {
            MessageBox.Show((sal.SelectedItem as TextBlock).Text);
        }
    }

    // Or we could use the EventArgs:
    if (e.AddedItems.Count == 1)
    {
        MessageBox.Show((e.AddedItems[0] as TextBlock).Text);
    }
}