我稍微重写了你的代码。诀窍是使用 MouseCapture 来避免进行多个事件处理(使用原始代码,由于按下鼠标按钮时布局发生变化,列表框一次单击最多可以选择三个选项)
这是代码:
主窗口.xaml
<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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Path=BoundText}" TextWrapping="Wrap" />
<ListBox Grid.Row="1"
ItemsSource="{Binding Path=BoundList}"
SelectedItem="{Binding Path=BoundListSelected, Mode=TwoWay}"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
namespace TextEditor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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 < 1000; i++)
{
boundList.Add(i.ToString());
}
InitializeComponent();
DataContext = this;
}
public List<string> BoundList { get { return boundList; } }
private List<string> boundList = new List<string>();
}
}
Edit :我实际上改变了 MainWindow 的编码方式(没有必要在 DependencyObject 上实现 INotifyPropertyChanged,所以我只是将其删除并设置两个依赖属性),但是您可以尝试通过在设置 BoundText 之前捕获鼠标来解决您的原始代码问题,然后释放它。