2011-01-11 76 views
0

我正在使用VS 2010 Express版windows phone的windows phone 7上工作。我有使用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> 
+0

wrappanel沒有選擇的事件和圖像不具有`ID`財產。你可以請張貼顯示你想要做什麼的XAML? – 2011-01-11 09:14:04

+0

更新了我的問題.. – Shaireen 2011-01-11 09:51:20

+0

感謝您添加XAML,並且我提供了一個答案,但是您仍然不會顯示wrappanel,「id」(您是指索引號?)還是什麼操作事件處理程序連接到。 – 2011-01-11 14:30:40

回答

2

您是否嘗試過使用SelectionChangedEventArgs從事件參數的e.AddedItems財產。

由於文檔說這會給你自從上次SelectionChanged事件發生被選中的

的項目。

具體來說,它會給你一個選定的綁定對象的IList。這不會給你索引,但是一旦你有了選定的項目,如果這是你真正想要的索引(例如IndexOf,你已經綁定了什麼),它可以很容易地獲得索引。

您也可以將發件人強制轉換爲列表框,然後檢查SelectedIndex,但這與列表框有關。

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在以下幾個方面的文字。
(請注意,使用一個消息純粹是用於演示。)(從工具包)

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); 
    } 
}