Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox Not Selecting the Selected Item
    primarykey
    data
    text
    <p>See code below<br> Visual Studio 2010<br> Above the ListBox have a TextBox.<br> Via binding the TextBox can get bigger or smaller when an item is selected.<br> That causes the ListBox to move.<br> When the ListBox moves the selected item is NOT the item that was clicked.<br> The selected item is the item under the mouse on the moved ListBox.<br> Some times it will not even select at all (try and go from 9 to 10 or from 10 to 9).<br> In this code to reproduce the problem even and odd produce different lengths.<br> So if you go from odd to odd or even to even then no problem.<br> If you go from on odd at the top an even at the bottom (without scrolling) then sometimes an item that was not even in view will be selected.<br> In the real code the TextBox is a description of the item and the descriptions are different lengths.<br> Interesting is in debug and there is a break point on get { return boundText; } then it does select the proper item.<br> I think it processes the select, then measures out the UI, and then processes the select a second time on the new UI.<br> Since it behaves different in debug it is hard to figure out. </p> <pre><code>&lt;Window x:Class="ListBoxMissClick.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=BoundText}" TextWrapping="Wrap" /&gt; &lt;ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Path=BoundList}" SelectedItem="{Binding Path=BoundListSelected, Mode=TwoWay}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; using System.ComponentModel; namespace ListBoxMissClick { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window, INotifyPropertyChanged { private string boundListSelected; private string boundText = string.Empty; private List&lt;string&gt; boundList = new List&lt;string&gt;(); private bool shortLong = true; public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public MainWindow() { for (int i = 0; i &lt; 1000; i++) { boundList.Add(i.ToString()); } InitializeComponent(); } public string BoundText { get { return boundText; } set { if (boundText != value) { boundText = value; NotifyPropertyChanged("BoundText"); } } } public List&lt;string&gt; BoundList { get { return boundList; } } public string BoundListSelected { get { return boundListSelected; } set { boundListSelected = value; if (Int32.Parse(value) % 2 == 0) { BoundText = value.ToString() + " something very long something very long something very long something very long something very long something very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very long"; } else { BoundText = value.ToString() + " something short "; } } } private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { BoundText = " something very long something very long something very long something very long something very long something very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very long"; } } } </code></pre> <p>In addition to the accepted answer Mouse.Capture and ReleaseMouseCapture work.</p> <pre><code>set { Mouse.Capture(this); { boundListSelected = value; if (Int32.Parse(value) % 2 == 0) { BoundText = value.ToString() + " something very long something very long something very long something very long something very long something very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very longsomething very long something very long something very long something very long something very long"; } else { BoundText = value.ToString() + " something short "; } } ReleaseMouseCapture(); } </code></pre>
    singulars
    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.
 

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