ListView jumps or does not shrink

↘锁芯ラ 提交于 2019-12-01 07:04:33
Chris

It took a good bit of playing with your example, but I eventually realised what the root cause of the behaviour was, and what to go looking for.

I think you're experiencing an issue caused by your CheckBox gaining focus when clicked, and raising a RequestBringIntoView event, which will essentially cause the ScrollViewer to kick the control containing your CheckBox into the "correct" place. The mechanics are explained in this answer: Stop WPF ScrollViewer automatically scrolling to perceived content.

If that scroll happens whilst you're clicking on a CheckBox, it will move out from under your cursor before you can unclick the mouse (you can demonstrate that by holding the mouse down when the problem occurs, and subsequently moving your mouse over the CheckBox in question, and releasing it).

You could create a simple event handler for the RequestBringIntoView event and use it with the CheckBox within your DataTemplate, and suppress the event using the Handled property, so it isn't propagated any further (the below worked for me).

XAML:

<DataTemplate>
    <CheckBox Width="125" IsChecked="{Binding Path=On}" RequestBringIntoView="RequestBringIntoViewSuppressor">
        <TextBlock Text="{Binding Path=StrValue}" TextWrapping="Wrap"/>
    </CheckBox>
</DataTemplate>

C#:

private void RequestBringIntoViewSuppressor(object sender, RequestBringIntoViewEventArgs e)
{
    e.Handled = true;
}

Your CheckBox would now no longer correctly bring itself into view if it was slightly off the screen, but it wouldn't jump unexpectedly, and you could customise the handler further if required.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!