我有一个StackPanel,它包含一个TextBox和一个组合框。
当我在文本框中设置焦点时(不是第一个),StackPanel的内容就会“跳”到顶部。
下面是密码。我对此进行了研究,并发布了我发现并尝试过的(但没有worK)。
我想防止“跳跃”。
所以运行下面的代码。滚动垂直条,直到您看到:
Name Three <<Text Box
(No Selection) ComboBox \/
Name Four <<Text Box
(No Selection) ComboBox \/现在,将光标放在“名称四”文本box.......and中,观察它“跳转”到顶部。(你现在看不到三和四,你看到了四和五。)
在现实生活中,我的堆叠板比这个要复杂得多,它让我的终端用户抓狂。
谢谢。
MainWindow.xaml
<Window x:Class="ListBoxControlSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="160" Width="250">
<Grid Margin="10">
<ListBox ItemsSource="{Binding Models}" SelectionMode="Single" RequestBringIntoView="FrameworkElement_OnRequestBringIntoView" SelectionChanged="Selector_OnSelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Name}"/>
<ComboBox VerticalContentAlignment="Top" VerticalAlignment="Top" Grid.Column="1" ItemsSource="{Binding Options}" >
</ComboBox>
<TextBlock Text="{Binding Title}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListBoxControlSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private IList<Model> _models;
public IList<Model> Models
{
get
{
return _models ?? (_models = new List<Model>
{
new Model{ Name = "Name One", Title = "Title One"},
new Model{ Name = "Name Two", Title = "Title Two"},
new Model{ Name = "Name Three", Title = "Title Three"},
new Model{ Name = "Name Four", Title = "Title Four"},
new Model{ Name = "Name Five", Title = "Title Five"},
new Model{ Name = "Name Six", Title = "Title Six"},
new Model{ Name = "Name Seven", Title = "Title Seven"}
});
}
}
private void FrameworkElement_OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//throw new NotImplementedException();
}
private void Selector_OnSelected(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
}
}
public class Model
{
public string Name { get; set; }
public string Title { get; set; }
private IList<string> _options;
public IList<string> Options
{
get
{
return _options ?? (_options = new List<string>
{
"left",
"right",
"both"
});
}
}
}
}我的发现和尝试(以防止跳跃)
<DataTemplate>
<StackPanel ScrollViewer.CanContentScroll="False">发布于 2013-12-30 16:54:36
我猜这与默认的滚动行为有关,即每当被选中时都会显示完整的项。
尝试禁用滚动,并将其包装在另一个ScrollViewer中:
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True" Height="250">
<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled">
...
</ListBox>
</ScrollViewer>这样,它将尝试将整个StackPanel滚动到视图中,而不是试图一次滚动每个单独的ListBox到视图中,并确保整个项目是可见的,这超出了分配给ScrollViewer的高度,因此它将使用您想要的更流畅的基于内容的滚动。
应该注意的是,这将同时呈现整个ListBox,而不会产生任何病毒化,因此,如果您有很多行,并且依赖于虚拟化来提高性能,那么使用此方法是不可取的。但根据你的问题听起来不像是你的案子。
我也不是积极的,但你可能必须把它嵌套在一个多一个面板,以及允许ListBox增长到它想要的任何高度。如果需要,请参阅这个答案获得更多详细信息。
发布于 2013-12-30 20:44:13
这是另一个答案。它适用于一个简单的WPF。
然而,我的真实情况有一个完整的控制和瑞秋的答案有效。
不过,为了完整起见,我想贴上这篇文章。
我正在粘贴这个网址上的代码:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/a3532b1f-d76e-4955-b3da-84c98d6d435c/annoying-auto-scroll-of-partially-displayed-items-in-wpf-listbox?forum=wpf
以下是代码:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="RequestBringIntoView" Handler="ListBoxItem_RequestBringIntoView"/>
</Style>
</ListBox.ItemContainerStyle>和
void ListBoxItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}发布于 2013-12-30 16:34:13
如果单击最后一个可见行的TextBox,则效果是可重复的。如果该行没有完全滚动到视图中,列表框将自动将该行滚动到视图中,使其完全可见。这反过来意味着所有的行向上滚动一行,这让你感觉到跳跃的效果。据我所知,你不能改变这种行为。
https://stackoverflow.com/questions/20842630
复制相似问题