在我的申请中,我有一个ListBox项目.该应用程序是用WPF编写的.
如何自动滚动到最后添加的项目?我希望在ScrollViewer添加新项目时将其移动到列表的末尾.
有什么事ItemsChanged吗?(我不想使用这个SelectionChanged活动)
我有一个WPF应用程序,其中包含一个多行TextBox,用于显示调试文本输出.
如何设置TextBox以便将文本附加到框中,它会自动滚动到文本框的底部?
我试图向后滚动一个WPF DataGrid代码.我用
int itemNum=0;
private void Down_Click(object sender, RoutedEventArgs e)
{
if (itemNum + 1 > dataGridView.Items.Count - 1) return;
itemNum += 1;
dataGridView.UpdateLayout();
dataGridView.ScrollIntoView(dataGridView.Items[itemNum]);
}
Run Code Online (Sandbox Code Playgroud)
仅当itemNum当前未显示行时,此选项才会向下滚动.
例如,如果DataGrid足够长以容纳10行而我有20行,我需要调用此函数11次(直到itemNum11)才能滚动到下一行.
如果行已经适合网格(即使它是屏幕上的最后一行),它也不会向下滚动.
我希望实现这一点,当我调用此方法时,网格将下一行放入网格的顶部(如滚动条所做).为什么它不起作用?
我的目标是为FlowDocumentScrollViewer创建可重用的附加行为,以便每当FlowDocument更新(附加)时,查看器自动滚动到末尾.
到目前为止的问题
我意识到那些可能是3个独立的问题(也就是问题).但是它们彼此依赖,并且我尝试了这种行为的整体设计.我问这是一个单一的问题,以防我以错误的方式解决这个问题.如果我是,那么正确的方法是什么?
/// Attached Dependency Properties not shown here:
/// bool Enabled
/// DependencyProperty DocumentProperty
/// TextRange MonitoredRange
/// ScrollViewer ScrollViewer
public static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d == null || System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
return;
DependencyProperty documentProperty = null;
ScrollViewer scrollViewer = null;
if (e.NewValue is bool && (bool)e.NewValue)
{
// Using reflection so that this will work with similar types.
FieldInfo documentFieldInfo = d.GetType().GetFields().FirstOrDefault((m) => m.Name == "DocumentProperty");
documentProperty = documentFieldInfo.GetValue(d) as DependencyProperty;
// …Run Code Online (Sandbox Code Playgroud)