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

  • SEARCH
  • Home
  • 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 6053989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:06:07+00:00 2026-05-23T08:06:07+00:00

I have a grid bound to a list of objects, the SelectedIndex is bound

  • 0

I have a grid bound to a list of objects, the SelectedIndex is bound to a property, I wish this SelectedIndex which works correctly as the index changes on sorting, however the collection within the model does not get sorted, so the index does not relate to the model.

Markup in view:

<DataGrid Name="gridCustomers"
          ItemsSource="{Binding Customers}"
          SelectedIndex="{Binding SelectedIndex}"
          CanUserSortColumns="True"
          SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Customer" Binding="{Binding ID}" />
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    </DataGrid.Columns>
</DataGrid>

Code in model

public ObservableCollection<Customer> Customers {
    get;
    private set;
}

private int selectedIndex;
public int SelectedIndex {
    get { return selectedIndex; }
    set {
        if (selectedIndex != value) {
            selectedIndex = value;
            OnNotifyPropertyChanged("SelectedIndex");
            OnNotifyPropertyChanged("SelectedCustomer");
        }
    }
}

public Customer SelectedCustomer {
    get { return CustomersView[selectedIndex]; }
}

I’m using this approach because I have “next/previous” buttons and a label binding to “x of y customers”. The SelectedCustomer is a convenience property for a child control which in this case shows the incorrect object.

  • 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-23T08:06:08+00:00Added an answer on May 23, 2026 at 8:06 am

    I’ve managed to solve this by using both SelectedIndex and SelectedItem binding properties and not navigating through the list itself.

    View:

    <DataGrid Name="gridCustomers"
                ItemsSource="{Binding Path=Customers}"
                SelectedIndex="{Binding SelectedIndex}"
                SelectedItem="{Binding SelectedCustomer}"
    

    Model:

    public ObservableCollection<Customer> Customers {
        get;
        private set;
    }
    
    private int selectedIndex;
    public int SelectedIndex {
        get { return selectedIndex; }
        set {
            if (selectedIndex != value) {
                selectedIndex = value;
                OnNotifyPropertyChanged("SelectedIndex");
                OnNotifyPropertyChanged("NavText");
            }
        }
    }
    
    private Customer selectedCustomer;
    public Customer SelectedCustomer {
        get { return selectedCustomer; }
        set {
            if (!Object.Equals(selectedCustomer, value)) {
                selectedCustomer = value;
                OnNotifyPropertyChanged("SelectedCustomer");
            }
        }
    }
    
    public string NavText {
        get {
            if (Customers.Count == 0)
                return "No Customers";
            if (SelectedIndex == -1)
                return String.Format("{0} customers", customers.Count);
            return String.Format("{0} of {1}", SelectedIndex + 1, Customers.Count);
        }
    }
    
    public void First() {
        if (Customers.Count == 0)
            return;
        SelectedIndex = 0;
    }
    
    public void Last() {
        if (Customers.Count == 0)
            return;
        SelectedIndex = Customers.Count - 1;
    }
    
    public void Next() {
        if (Customers.Count == 0 || SelectedIndex == Customers.Count - 1)
            return;
        SelectedIndex++;
    }
    
    public void Previous() {
        if (Customers.Count == 0 || SelectedIndex <= 0)
            return;
        SelectedIndex--;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid bound to a BindingSource which is bound to DataContext table,
I have a grid (DevExpress XtraGrid, if that matters) which is bound to a
I have a list of objects bound to a ListView in Windows Phone 7
I have a ListBox that is bound to a list of CustomerViewModel-objects, that each
I have a list of objects in C# that have a property called 'Sequence'.
I have a List<T> collection which is bound to a DataGrid. T is some
I have a data grid in a view that is bound to a List
I have a Generic List of objects that I've bound to a custom control.
I have 2 silverlight controls on a form; datagrid which is bound to list
I have a DataGridView bound to an array of objects. List<Node> nodes = GetNodeList();

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.