Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox.SelectedIndex = -1 is not unsetting the selected item
    primarykey
    data
    text
    <p>I have a listbox defined in XAML is follows:</p> <pre><code>&lt;ListBox x:Name="listLeagues" DisplayMemberPath="LeagueName" SelectionChanged="listLeagues_SelectionChanged" SelectedItem="{Binding SelectedObject, Mode=TwoWay}" SelectionMode="Single" BorderBrush="{StaticResource noBrush}" Background="{StaticResource noBrush}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"&gt; &lt;/ListBox&gt; </code></pre> <p>In code I set the DataContext as follows: <code>listLeagues.ItemsSource = Leagues;</code> where 'Leagues' is a custom collection. I am trying to change the selected Item in code with the following code:</p> <pre><code> private void addButton_Click(object sender, MouseButtonEventArgs e) { FantasyLeague addedLeague = new FantasyLeague(); Random rnd = new Random(); addedLeague.LeagueName = "New Leauge " + rnd.Next(100).ToString(); Leagues.Add(addedLeague); listLeagues.SelectedIndex = -1; listLeagues.SelectedItem = addedLeague; LeagueDetailsClickHelper(); } </code></pre> <p>My listbox has SelectionMode = Single. The line <code>listLeagues.SelectedIndex = -1;</code> has no affect and when changing the SelectedItem to a new value it has both items as SelectedItems even though the mode is single. Can someone explain what is going wrong?</p> <p>Thanks so much for your help. </p> <p>EDIT</p> <p>I have removed the <code>SelectedItem="{Binding SelectedObject, Mode=TwoWay}"</code> from the XAML and found that things are working properly [most of the time]. I am updating the underlying source of the ListBox and when changing values that affect the == operator assessment to be different from the original values then I have noticed the SelectionChanged event doesn't include anything in the RemovedItems. I am going to attempt to make a deep copy of the object and change the deep copy and see if that solves my issue. </p> <p>FINAL UPDATE</p> <p>I made a deep copy of the FantasyLeague that is in the Leagues collection. I have members of the FantasyLeauge bound to a form which can be changed. When I made the deep copy of this the source of the form that allowed changes to the Leagues collection instead of the original there were no problems. I'm not sure why changing values that are linked to the assessment of == and .Equals would cause this behavior and not just update the target/source correctly, however, I have worked around the issue. If anyone has input on what causes this behavior I would appreciate it. Thanks for everyone that made suggestions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload