Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1051369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:55:11+00:00 2026-05-16T16:55:11+00:00

Given the following XAML code that with a ListControl like behavior: <StackPanel> <ItemsControl Name=_listbox

  • 0

Given the following XAML code that with a ListControl like behavior:

    <StackPanel>
        <ItemsControl Name="_listbox" ItemsSource="{Binding ElementName=_userControl, Path=DataContext}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                         ...
                    </DockPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>

Since the list can be long (100-200 items), and the items look similar, I think it would helpful for the user during scrolling if every item would display their position in the list. How could an item in the template know its own position in the list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:55:12+00:00Added an answer on May 16, 2026 at 4:55 pm

    Here is a hack solution. We can use Value Conversion with DataBinding. So the first step is to declare our ValueConvertor:

    public class ListItemToPositionConverter : IValueConverter
        {
            #region Implementation of IValueConverter
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var item = value as ListBoxItem;
                if (item != null)
                {
                    var lb = FindAncestor<ListBox>(item);
                    if (lb != null)
                    {
                        var index = lb.Items.IndexOf(item.Content);
                        return index;
                    }
                }
                return null;
            }            
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    

    Declare wherever you want this static method in order to obtain ListBox parent:

    public static T FindAncestor<T>(DependencyObject from) where T : class
            {
                if (from == null)
                    return null;
    
                var candidate = from as T;
                return candidate ?? FindAncestor<T>(VisualTreeHelper.GetParent(from));
            }
    

    Then in ListBox.Resources declare our convertor as follows:

    <ListBox.Resources>
                    <YourNamespace:ListItemToPositionConverter x:Key="listItemToPositionConverter"/>
                </ListBox.Resources>
    

    And finally – DataTemplate:

    <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Converter={StaticResource listItemToPositionConverter}}"/>
                            <Label Content="{Binding Path=DisplayName}"></Label>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
    

    Note: in this example the items will be numerated starting with 0 (zero), you can change it in Convert method by adding 1 to result.

    Hope this helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following XAML... <ComboBox x:Name=advisoriesComboBox DisplayMemberPath=Name ItemsSource={Binding Path=Advisories} SelectedItem={Binding Path=SelectedAdvisory} /> <Button Command={Binding
Given the following XAML: <TabControl DataContext={StaticResource mainVM} ItemsSource={Binding MovableVM.Docked, UpdateSourceTrigger=PropertyChanged}> <TabControl.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock
I have the following xaml: <ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <Button Content={Binding Name}></Button> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
given the following XAML code: <Canvas Name=MainView> <Canvas Name=TriangleElement Width=50 Height=50 Canvas.Left=110 Canvas.Top=100> <Canvas.RenderTransform>
Given the following xaml code, I would like to create this xaml code the
The following code is part of a small XAML application that displays data in
Given the following code: <Window x:Class=WpfApplication76.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:col=clr-namespace:System.Collections;assembly=mscorlib Title=Window1 Height=300 Width=300> <Window.Resources> <CollectionViewSource
Given the following code: <MenuItem x:Name=MenuItem_Root Header=Root> <MenuItem x:Name=MenuItem_Item1 IsCheckable=True Header=item1 /> <MenuItem x:Name=MenuItem_Item2
Given the following bit of XAML <Border Name=Brder Visibility=Visible Width=10 Height=10 Background=Red></Border> <Button Content=Hide></Button>
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.