英文:
How to move to the top of a WPF Listbox?
问题
我在一个.xaml文件中有一个可滚动的ListBox,它绑定到一个视图模型中的可观察集合。
当新数据添加到集合中时,该数据会添加到ListBox的顶部。当ListBox包含大量数据并且我向下滚动ListBox时,新数据会添加到其顶部,但除非我使用滚动条滚动到顶部,否则我看不到它。
每次向可观察集合添加新项后,如何自动滚动到顶部?
WPF代码:
<ListBox Grid.Row="2"
Grid.Column="0"
ItemsSource="{Binding BagItems}"
ItemTemplateSelector="{StaticResource BagItemTemplateSelector}"
Grid.ColumnSpan="5"
Foreground="{DynamicResource DarkerGreyBrush}"
Background="{DynamicResource LightestGreyBrush}"
FontWeight="Medium"
HorizontalContentAlignment="Stretch"
ItemContainerStyle="{DynamicResource ListBoxContainerStyle}"
SelectedItem="{Binding SelectedItem}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
KeyDown="ListBox_KeyDown" ManipulationBoundaryFeedback="ListBox_ManipulationBoundaryFeedback">
<i:Interaction.Behaviors>
<behaviours:ListBoxSelectionModeOverrideBehaviour SupressKeyEvents="{Binding DialogController.DialogAvailable}" />
</i:Interaction.Behaviors>
</ListBox>
视图模型C#代码:
if (shoppingBagItem != null)
{
this.TryInvokeOnUiThread(() =>
{
this.BagItems.Insert(0, shoppingBagItem);
this.SelectedItem = shoppingBagItem;
});
}
英文:
I've got a scrollable ListBox in a .xaml file that's bound to some data - an observable collection in a view model.
When new data is added to the collection, that data gets added to the top of the ListBox. When the ListBox contains lots of data and I scroll down the ListBox, new data gets added to the top of it however I can't see it unless I use the scrollbar to scroll to the top.
How can I automatically scroll to the top after each new item is added to the observable collection?
WPF code:
<ListBox Grid.Row="2"
Grid.Column="0"
ItemsSource="{Binding BagItems}"
ItemTemplateSelector="{StaticResource BagItemTemplateSelector}"
Grid.ColumnSpan="5"
Foreground="{DynamicResource DarkerGreyBrush}"
Background="{DynamicResource LightestGreyBrush}"
FontWeight="Medium"
HorizontalContentAlignment="Stretch"
ItemContainerStyle="{DynamicResource ListBoxContainerStyle}"
SelectedItem="{Binding SelectedItem}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
KeyDown="ListBox_KeyDown" ManipulationBoundaryFeedback="ListBox_ManipulationBoundaryFeedback">
<i:Interaction.Behaviors>
<behaviours:ListBoxSelectionModeOverrideBehaviour SupressKeyEvents="{Binding DialogController.DialogAvailable}" />
</i:Interaction.Behaviors>
</ListBox>
View model C# code:
if (shoppingBagItem != null)
{
this.TryInvokeOnUiThread(() =>
{
this.BagItems.Insert(0, shoppingBagItem);
this.SelectedItem = shoppingBagItem;
});
}
答案1
得分: 1
以下是已翻译的内容:
以下的 Behavior
类会自动将 ListBox
中的 SelectedItem
滚动到视图中。
public class perListBoxHelper : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}
private static void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox == null)
{
return;
}
Action action = () =>
{
var selectedItem = listBox.SelectedItem;
if (selectedItem != null)
{
listBox.ScrollIntoView(selectedItem);
}
};
listBox.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
}
用法...
<ListBox ... >
<i:Interaction.Behaviors>
<vhelp:perListBoxScrollSelecionIntoViewBehavior />
</i:Interaction.Behaviors>
</ListBox>
更多详情请查看我的博客文章。
英文:
The following Behavior
class will automaticaly scroll the SelectedItem
of a ListBox
into view.
public class perListBoxHelper : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}
private static void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox == null)
{
return;
}
Action action = () =>
{
var selectedItem = listBox.SelectedItem;
if (selectedItem != null)
{
listBox.ScrollIntoView(selectedItem);
}
};
listBox.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
}
Useage ...
<ListBox ... >
<i:Interaction.Behaviors>
<vhelp:perListBoxScrollSelecionIntoViewBehavior />
</i:Interaction.Behaviors>
</ListBox>
More details on my blog post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论