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 563575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:40:30+00:00 2026-05-13T12:40:30+00:00

I’m having an issue with a ComboBox which is bound to an ObservableCollection and

  • 0

I’m having an issue with a ComboBox which is bound to an ObservableCollection and I was wondering if anyone can point to what I am missing.

I have a ComboBox which is bound to a simple ObservableCollection<string>. Also I bind the SelectedIndex in a OneWay binding to some property.

In my application I get to a point where I want to clear the collection and repopulate it with different data and setting the SelectedIndex to a new value. for some reason the SelectedIndex binding does not work.

I’m attaching a little repro of the problem:

public partial class Window1 : Window, INotifyPropertyChanged
{
    private int j;
    public event PropertyChangedEventHandler PropertyChanged;

    public Window1()
    {
        InitializeComponent();
        DataContext = this;
        Tables = new ObservableCollection<string>();
    }

    public ObservableCollection<string> Tables { get; set; }

    private int _TheIndex;
    public int TheIndex
    {
        get { return _TheIndex; }
        set
        {
            _TheIndex = value;
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TheIndex"));
            }
        }
    }

    private void aaaa(object sender, RoutedEventArgs e)
    {
        j = (j + 1)%10;
        Tables.Clear();
        for(int i = 0; i < 10 ; i++)
        {
            Tables.Add(i.ToString());
        }
        TheIndex = j;
    }
}

the xaml is :

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ComboBox x:Name="TablesCombobox"
                      ItemsSource="{Binding Tables}"
                      SelectedIndex="{Binding TheIndex, Mode=OneWay}"/>
            <Button Content="asdasd" Click="aaaa"/>
        </StackPanel>
    </Grid>
</Window>
  • 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-13T12:40:30+00:00Added an answer on May 13, 2026 at 12:40 pm

    The problem is entirely caused by the Tables.Clear() line in your aaaa() method. Since Tables is an observable collection, wiping out all of the contents of the collection causes WPF to update the display with a new, empty list. Then it tries to select the currently active item using SelectedIndex, which does not exist (because the list is now empty). As a result, the binding engine is left with a value that cannot be applied, and decides to deactivate and detach the binding logic:

    System.Windows.Data Warning: Got PropertyChanged event from Window1 for TheIndex
    System.Windows.Data Warning: GetValue at level 0 from Window1 using DependencyProperty(TheIndex): '1'
    System.Windows.Data Warning: TransferValue - got raw value '1'
    System.Windows.Data Warning: TransferValue - using final value '1'
    System.Windows.Data Warning: Deactivate
    System.Windows.Data Warning: Replace item at level 0 with {NullDataItem}
    System.Windows.Data Warning: Detach
    

    By the time it gets to the ‘TheIndex = j;’ line, the binding is no longer active and does not see the change to TheIndex, which means that desired index is no longer selected.

    There are a couple of solutions to solve this:

    1. Don’t blow away the entire collection every time. Without clearing the collection, the data binding logic always has an index to select, meaning it never detaches.
    2. Use a TwoWay binding. This works because now the ComboBox participates in binding; you clear Tables, the binding tries to set but can’t find the index so the ComboBox resets to the special ‘no index’ position of -1, which then writes back to TheIndex (the two-way part), which is a valid value so the binding logic doesn’t detach.
    3. Select no index (-1) before clearing the collection. If no index (-1) is selected when Tables is cleared, then ComboBox doesn’t try to apply SelectedItem, which means it doesn’t ‘see’ the collection emptied and re-filled, and therefore, doesn’t detach.

      private void aaaa(object sender, RoutedEventArgs e)
      {
          TheIndex = -1;
          j = (j + 1)%10;
          Tables.Clear();
          for (int i = 0; i < 10; i++)
          {
              Tables.Add(i.ToString());
          }
          TheIndex = j;
      }
      

    For performance, architectural, and clarity reasons, I’d highly recommend option 1, though I realize that your actual scenario may be more complex and require something along the lines of 3.


    Sidenote:

    Locating the reason behind binding issues like this is reasonably easy when using binding traces like the one posted above. Turn them on for a single binding by declaring the System.Diagnostics namespace and adding PresentationTraceSources.TraceLevel=Highto the binding that is causing trouble:

    <Window xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" />
    ...
    <TextBlock Text="{Binding Path=x, diag:PresentationTraceSources.TraceLevel=High}" />
    

    More ways of debugging WPF bindings are here.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.