Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've rewritten your code a bit. The trick is to use a MouseCapture to avoid having multiple event handling (with your original code, the listBox was getting up to three selection for a single click due to the layout changeing while mouse button was pressed)</p> <p>Here is the code : </p> <p><strong>MainWindow.xaml</strong></p> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="TextEditor.MainWindow" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBox Grid.Row="0" Text="{Binding Path=BoundText}" TextWrapping="Wrap" /&gt; &lt;ListBox Grid.Row="1" ItemsSource="{Binding Path=BoundList}" SelectedItem="{Binding Path=BoundListSelected, Mode=TwoWay}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>MainWindow.xaml.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; namespace TextEditor { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public string BoundText { get { return (string)GetValue(BoundTextProperty); } set { SetValue(BoundTextProperty, value); } } // Using a DependencyProperty as the backing store for BoundText. This enables animation, styling, binding, etc... public static readonly DependencyProperty BoundTextProperty = DependencyProperty.Register("BoundText", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty)); public string BoundListSelected { get { return (string)GetValue(BoundListSelectedProperty); } set { SetValue(BoundListSelectedProperty, value); } } // Using a DependencyProperty as the backing store for BoundListSelected. This enables animation, styling, binding, etc... public static readonly DependencyProperty BoundListSelectedProperty = DependencyProperty.Register("BoundListSelected", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty, OnBoundListSelectedChanged)); private static void OnBoundListSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var mainWindow = d as MainWindow; var value = e.NewValue as string; Mouse.Capture(mainWindow); if (Int32.Parse(value) % 2 == 0) { mainWindow.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 { mainWindow.BoundText = value.ToString() + " something short "; } mainWindow.ReleaseMouseCapture(); } public MainWindow() { for (int i = 0; i &lt; 1000; i++) { boundList.Add(i.ToString()); } InitializeComponent(); DataContext = this; } public List&lt;string&gt; BoundList { get { return boundList; } } private List&lt;string&gt; boundList = new List&lt;string&gt;(); } } </code></pre> <p><strong>Edit :</strong> I actually changed the way MainWindow was coded (it's not necessary to implement INotifyPropertyChanged on a DependencyObject, so i just removed it and set two dependency properties) but you could try solving your issue with your ogirinal code by simply capturing the mouse before setting BoundText, and then releasing it.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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