2012-07-13 33 views
1

我想要一個元素或控件來顯示readyonly,色彩鮮豔,可選擇,可滾動的文本,這是我的應用程序中的一種日誌。我不知道它是否是fixed document or flow document哪個控件在WPF中顯示只讀文本?

RichText可能是看似選擇,但它最初支持編輯。我相信即使我設置readonly = true,內置編輯支持也需要一些資源。我想找到一個輕量級的。

也許FlowDocumentScrollViewer?它是隻讀的,默認情況下不顯示工具欄。即使我打開了IsToolBarVisible,工具欄也只是一個小小的控件。

該塊進入我的腦海。雖然它可能是最輕的控件,但我無法在其中選擇文本。

也許有其他選擇嗎?你有什麼意見?

+0

您應該能夠使用'IsReadOnly'財產上的大多數控件。 – 2012-07-13 01:56:37

+1

@DougRamsey然而,內建的編輯支持需要一些資源;我想找到一個輕量級的,就像我在我的問題中所說的那樣。 – Gqqnbig 2012-07-13 01:58:35

+0

如果你想要輕量級的,WPF應用程序可以得到一些大(當然相對來說)。也許你想考慮像MFC應用程序這樣的應用程序,它具有最小的依賴關係,並且通常使用更少的內存。只是一個想法。 – 2012-07-13 02:20:27

回答

1

我做了一個實驗,幫助我在FlowDocumentScrollViewer,RichTextBox和TextBlock中選擇我喜歡的控件。我發現FlowDocumentScrollViewer是最好的。

在每個窗口中,我有兩個相同類型的控件:FlowDocumentScrollViewer,RichTextBox或TextBlock。我做了三個這樣的窗口,因爲MainWindow有三個按鈕。 enter image description here

private void prepareButton_Click(object sender, RoutedEventArgs e) 
{ 

    document1 = HelperClass.GetDocument(); 

    document2 = HelperClass.GetDocument(); 
} 

private void loadButton_Click_1(object sender, RoutedEventArgs e) 
{ 
    Stopwatch watch = new Stopwatch(); 
    watch.Start(); 

    viewer1.Document = document1; 
    viewer2.Document = document2; 

    this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, 
        new Action(() => 
        { 
         watch.Stop(); 
         MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms",Title); 
        })); 
} 

凡viewer1和viewer2可以FlowDocumentScrollViewer或RichTextBox的。 對於TextBlock的,我用

private void prepareButton_Click(object sender, RoutedEventArgs e) 
{ 

    inlines1 = HelperClass.GetInlines(); 

    inlines2 = HelperClass.GetInlines(); 
} 

private void loadButton_Click_1(object sender, RoutedEventArgs e) 
{ 
    Stopwatch watch = new Stopwatch(); 
    watch.Start(); 

    viewer1.Inlines.AddRange(inlines1); 
    viewer2.Inlines.AddRange(inlines2); 


    this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, 
        new Action(() => 
        { 
         watch.Stop(); 
         MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms"); 
        })); 
} 

試驗表明FlowDocumentScrollViewer有三間最佳性能:

   FlowDocumentScrollViewer RichTextBox TextBlock 
Working set  65400     67252   82124 
Loading Time  1045     1414   45119 
+0

除非您實現手動垃圾回收,否則不能保證RichTextBox和TextBlock內存讀數不包含FlowDocumentScrollViewer中的內存。請記住,.NET垃圾回收不會立即清理。 – BTownTKD 2012-07-13 18:22:05

0

我不確定您認爲正在通過「編輯」功能佔用的資源類型。選擇文本的能力與編輯文本的能力結合在一起。

如果你想要一個,你必須忍受另一個。 Luckilly將IsReadOnly設置爲「True」將滿足您的功能要求。

如果您的應用程序計算機能夠使用WPF運行.NET Framework,那麼我不會擔心編輯簡單文本的能力(或可能無法使用)的少量資源。