以下のコードを参照してください
Visual Studio 2010
ListBox の上に TextBox があります。
バインディングを介して、アイテムが選択されたときに TextBox を大きくしたり小さくしたりできます。
これにより、ListBox が移動します。
ListBox が移動するとき、選択されたアイテムはクリックされたアイテムではありません。
選択された項目は、移動した ListBox でマウスの下にある項目です。
まったく選択されない場合もあります (9 から 10 または 10 から 9 に変更してみてください)。
このコードでは、問題を再現するために、偶数と奇数で異なる長さが生成されます。
したがって、奇数から奇数、または偶数から偶数に移行しても問題ありません。
上が奇数から下が偶数 (スクロールせずに) に移動すると、表示されていない項目が選択されることがあります。
実際のコードでは、TextBox は項目の説明であり、説明の長さは異なります。
興味深いのはデバッグで、get { return boundText; にブレークポイントがあります。適切な項目を選択します。
選択を処理し、UI を測定してから、新しい UI でもう一度選択を処理すると思います。
デバッグでは動作が異なるため、把握するのは困難です。
<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}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=BoundText}" TextWrapping="Wrap" />
<ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Path=BoundList}" SelectedItem="{Binding Path=BoundListSelected, Mode=TwoWay}"/>
</Grid>
</Window>
using System.ComponentModel;
namespace ListBoxMissClick
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string boundListSelected;
private string boundText = string.Empty;
private List<string> boundList = new List<string>();
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 < 1000; i++)
{
boundList.Add(i.ToString());
}
InitializeComponent();
}
public string BoundText
{
get { return boundText; }
set
{
if (boundText != value)
{
boundText = value;
NotifyPropertyChanged("BoundText");
}
}
}
public List<string> 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";
}
}
}
受け入れられた回答に加えて、 Mouse.Capture と ReleaseMouseCapture が機能します。
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();
}