【问题标题】:How to get the index of an item added dynamically to a ListBox如何获取动态添加到 ListBox 的项目的索引
【发布时间】:2012-09-21 23:11:13
【问题描述】:

我想在动态添加的列表框中获取项目的当前索引。本质上,我的项目是一个 HubTile,并且 Hubtiles 一次添加一个点击事件。 Hubtile 项目的集合保存在 ObservableCollection 中。此 observablecolelction 绑定到显示项目的列表框。为了说明,我的内容如下:

TabsPage.xaml

<ListBox x:Name="tileList" Grid.Row="0" Margin="12,0,12,0" toolkit:TiltEffect.IsTiltEnabled="True">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <toolkit:HubTile Title="{Binding TileName}" Margin="6"
                                         Notification="{Binding Notification}"
                                         DisplayNotification="{Binding DisplayNotification}"
                                         Message="{Binding Message}"
                                         GroupTag="{Binding GroupTag}"
                                         Source="{Binding ImageUri}"
                                         Tap="hubTile_Tap">
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="menu">
                                <toolkit:MenuItem Header="pin to start" Tap="MenuItem_Tap"/>
                                <toolkit:MenuItem Header="delete" Tap="deleteMenuItem_Tap"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </toolkit:HubTile>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

TabsPage.xaml.cs

ObservableCollection<TileItem> tileItems;

public TabsPage()
    {
        InitializeComponent();

        CreateTileList(); //starts the list of HubTiles with a single main tile
    }

private void CreateTileList()
    {
        tileItems = new ObservableCollection<TileItem>()             
        {            
            //TileItem is     
            new TileItem() { ImageUri = mainImage, Title = "main", /*Notification = "",*/ Message = "main", GroupTag = "MainGroup", TileName = "main" },

        };

        //Set the first tile item
        this.tileList.ItemsSource = tileItems;  //sets the tileList Listbox ItemsSource to the tileItems ObservableCollection            

    }

void addNew_Click(object sender, EventArgs e)
    {
        BitmapImage newTileImage = new BitmapImage();

        var newItem = new TileItem() { ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new HubTile", GroupTag = "TileGroup", TileName = "new" };
        tileItems.Add(newItem); //update UI immediately and add to collection
    }

private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //how to get the current index of the tapped HubTile//
        //var tap = (((sender as HubTile).Parent as ListBox).SelectedIndex); //NullReferenceException thrown
    }

所以,HubTile 项目已正确添加到 UI,但在点击事件上我不知道如何获取当前点击的 HubTile 项目的选定索引?

【问题讨论】:

    标签: c# windows-phone-7 xaml listbox


    【解决方案1】:

    你可以像这样使用被点击的项目的DataContext

    private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;
        TileItem item = (TileItem)element.DataContext;
        int index = tileItems.IndexOf(item);
        // Use the index
    }
    

    您也可以订阅 ListBox 的 SelectionChanged 事件,而不是 Hubtile 的 Tap 事件。然后在 Selectionchanged 事件中获取索引。

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            int index = tileList.SelectedIndex;
        }
    

    【讨论】:

      【解决方案2】:

      使 tileItems 成为公共属性并在 XAML 中将其绑定到它作为源。

      添加一个公共 int 并将路径绑定到它作为 SelectedIndex

      <ListBox ItemsSource="{Binding Path=Rules}" SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay}">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 2010-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多