![](/img/trans.png)
[英]WPF FlowDocumentScrollViewer automatically scroll to bottom of the document
[英]Scroll a WPF FlowDocumentScrollViewer from code?
我有一個FlowDocumentScrollViewer我想在添加文本時自動滾動到底部。
<FlowDocumentScrollViewer Name="Scroller">
<FlowDocument Foreground="White" Name="docDebug" FontFamily="Terminal">
<Paragraph Name="paragraphDebug"/>
</FlowDocument>
</FlowDocumentScrollViewer>
在代碼中我向段落添加了內聯,但是當有很多文本時,我希望能夠簡單地使用代碼向下滾動而不是讓用戶這樣做。
有什么建議?
嘗試:
Scroller.ScrollViewer.ScrollToEnd();
其中“Scroller”是FlowDocumentScrollViewer的名稱。
編輯 :我寫的這個答案有點太快了。 FlowDocumentScrollViewer不公開ScrollViewer屬性。 我實際上擴展了FlowDocumentScrollViewer類並自己實現了ScrollViewer屬性。 這是實施:
/// <summary>
/// Backing store for the <see cref="ScrollViewer"/> property.
/// </summary>
private ScrollViewer scrollViewer;
/// <summary>
/// Gets the scroll viewer contained within the FlowDocumentScrollViewer control
/// </summary>
public ScrollViewer ScrollViewer
{
get
{
if (this.scrollViewer == null)
{
DependencyObject obj = this;
do
{
if (VisualTreeHelper.GetChildrenCount(obj) > 0)
obj = VisualTreeHelper.GetChild(obj as Visual, 0);
else
return null;
}
while (!(obj is ScrollViewer));
this.scrollViewer = obj as ScrollViewer;
}
return this.scrollViewer;
}
}
我遇到了類似的問題:我想要一個文本區域 ,它可以保存我的文本,能夠包裝它,它填充其父控件並可滾動。
首先,我嘗試使用帶有ScrollViewer的TextBlock ,我認為它有效,但出於某種原因,我想使用FlowDocument而不是FlowDocumentScrollViewer 。 后者不起作用,我只是無法放棄戰斗,所以我試圖找到解決方案,這就是我到這里的方式。 我已經嘗試應用原始問題的答案中提供的解決方法,但是我找不到任何解決方案(我使用的是.NET 4.5,也許它適用於其他版本,但我不知道這一點)。
我也試過單獨使用一個FlowDocument ,但是控件包含了一些我不想要的UI元素。 所以,我提出了另一個解決方案。
<ScrollViewer VerticalScrollBarVisibility="Auto">
<FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
<FlowDocument>
那就對了。 有用! 調用ScrollViewer.ScrollToBottom()就可以了! 所述的ScrollViewer使得能夠滾動和FlowDocumentScrollViewer將刪除的FlowDocument的UI元素。 希望能幫助到你!
顯然我的構造有一個缺陷,因為這樣FlowDocument不能通過鼠標的滾輪滾動。 但是,將FlowDocumentScrollViewer控件的IsHitTestVisible屬性設置為False可以解決此問題。
這里給出的其他答案有點令人費解,因為我在FlowDocumentScrollViewer上看不到任何公共“ScrollViewer”屬性。
我這樣解決了這個問題。 請注意,此方法在初始化期間可以返回null:
public static ScrollViewer FindScrollViewer(this FlowDocumentScrollViewer flowDocumentScrollViewer)
{
if (VisualTreeHelper.GetChildrenCount(flowDocumentScrollViewer) == 0)
{
return null;
}
// Border is the first child of first child of a ScrolldocumentViewer
DependencyObject firstChild = VisualTreeHelper.GetChild(flowDocumentScrollViewer, 0);
if (firstChild == null)
{
return null;
}
Decorator border = VisualTreeHelper.GetChild(firstChild, 0) as Decorator;
if (border == null)
{
return null;
}
return border.Child as ScrollViewer;
}
這可能是一個非常晚的答案,但我找到了一種方法來做到這一點。
//after your FlowDocumentScrollViewer(for example, x:Name="fdsv") loaded
ScrollViewer sv = fdsv.Template.FindName("PART_ContentHost", fdsv) as ScrollViewer;
sv.ScrollToBottom();
sv.ScrollToTop();
sv.ScrollToVerticalOffset(100);
// etc.
檢查IScrollInfo和ScrollViewer以獲取詳細信息。
我希望這可以幫助你。
7年前問了這個問題,現在我遇到了同樣的問題,我找到了一個簡單的解決方案。 以下代碼將一個Section添加到Flowdocument,它與Paragraph相同,然后滾動到結尾。
private void addSection(Section section)
{
section.Loaded += section_Loaded;
fdoc.Blocks.Add(section);
}
private void section_Loaded(object sender, RoutedEventArgs e)//scroll to end
{
var sec = sender as Section;
if (sec != null)
{
sec.BringIntoView();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.