Displaying streaming rich text with WPF

后端 未结 3 1704
死守一世寂寞
死守一世寂寞 2021-02-11 00:58

I have a WPF application that connects via a socket to a device and gets streaming text data (approx 1 message per second). This data is then displayed on the UI. The user can

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-11 01:07

    This idea complicates things significantly, but my thought would be to create one viewer per message and create only as many viewers as are necessary to display the currently visible messages. I think the VirtualizingStackPanel control would be a good tool to manage this. I found a series describing the implementation of a VirtualizingStackPanel here.

    Obviously, this implies maintaining the message buffer in a separate data structure.

    EDIT: I just realized that the standard ListBox control uses a VirtualizingStackPanel in its implementation. With this in mind, my revised suggestion is:

    1. Create a data structure to contain the source of each message.
    2. Create a property on the data structure that creates a FlowDocument from the message source "on the fly"
    3. Bind a ListBox to a collection of said data structures.
    4. Define the ItemTemplate for the ListBox with a FlowDocumentScrollViewer where the Document property is bound to the aforementioned data structure's property.

    EDIT 2: Regarding printing/zooming: I can't help you much with printing in WPF (something involving VisualBrush, maybe?), but zooming should be pretty easy to do. I created a ZoomListBox to test this idea. The XAML looks like this:

    
        
            
                
                    
                        
                    
                
            
        
    
    

    And the code behind is this:

    public partial class ZoomListBox
    {
        public ZoomListBox()
        {
            this.InitializeComponent();
        }
    
        public double Zoom
        {
            get { return (double)GetValue(ZoomProperty); }
            set { SetValue(ZoomProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Zoom.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ZoomProperty =
            DependencyProperty.Register("Zoom", typeof(double), typeof(ZoomListBox), new UIPropertyMetadata(1.0));
    }
    

    And an example of using it:

    
        
            
            
        
        
            
            
            
            
            
            
            
        
         
    
    

提交回复
热议问题