Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I should start selling these WPF samples instead of giving them out for free. =P</p> <p><img src="https://i.stack.imgur.com/IB7sC.png" alt="enter image description here"></p> <ul> <li>Virtualized UI (Using <code>VirtualizingStackPanel</code>) which provides incredibly good performance (even with 200000+ items)</li> <li>Fully MVVM-friendly.</li> <li><code>DataTemplate</code>s for each kind of <code>LogEntry</code> type. These give you the ability to customize as much as you want. I only implemented 2 kinds of LogEntries (basic and nested), but you get the idea. You may subclass <code>LogEntry</code> as much as you need. You may even support rich text or images.</li> <li>Expandable (Nested) Items.</li> <li>Word Wrap.</li> <li>You can implement filtering, etc by using a <code>CollectionView</code>.</li> <li><p>WPF Rocks, just copy and paste my code in a <code>File -&gt; New -&gt; WPF Application</code> and see the results for yourself.</p> <pre><code>&lt;Window x:Class="MiscSamples.LogViewer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MiscSamples" Title="LogViewer" Height="500" Width="800"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="ItemsControl" x:Key="LogViewerStyle"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate&gt; &lt;ScrollViewer CanContentScroll="True"&gt; &lt;ItemsPresenter/&gt; &lt;/ScrollViewer&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ItemsPanel"&gt; &lt;Setter.Value&gt; &lt;ItemsPanelTemplate&gt; &lt;VirtualizingStackPanel IsItemsHost="True"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;DataTemplate DataType="{x:Type local:LogEntry}"&gt; &lt;Grid IsSharedSizeScope="True"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition SharedSizeGroup="Index" Width="Auto"/&gt; &lt;ColumnDefinition SharedSizeGroup="Date" Width="Auto"/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Text="{Binding DateTime}" Grid.Column="0" FontWeight="Bold" Margin="5,0,5,0"/&gt; &lt;TextBlock Text="{Binding Index}" Grid.Column="1" FontWeight="Bold" Margin="0,0,2,0" /&gt; &lt;TextBlock Text="{Binding Message}" Grid.Column="2" TextWrapping="Wrap"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:CollapsibleLogEntry}"&gt; &lt;Grid IsSharedSizeScope="True"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition SharedSizeGroup="Index" Width="Auto"/&gt; &lt;ColumnDefinition SharedSizeGroup="Date" Width="Auto"/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="{Binding DateTime}" Grid.Column="0" FontWeight="Bold" Margin="5,0,5,0"/&gt; &lt;TextBlock Text="{Binding Index}" Grid.Column="1" FontWeight="Bold" Margin="0,0,2,0" /&gt; &lt;TextBlock Text="{Binding Message}" Grid.Column="2" TextWrapping="Wrap"/&gt; &lt;ToggleButton x:Name="Expander" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Content="+" HorizontalAlignment="Right"/&gt; &lt;ItemsControl ItemsSource="{Binding Contents}" Style="{StaticResource LogViewerStyle}" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" x:Name="Contents" Visibility="Collapsed"/&gt; &lt;/Grid&gt; &lt;DataTemplate.Triggers&gt; &lt;Trigger SourceName="Expander" Property="IsChecked" Value="True"&gt; &lt;Setter TargetName="Contents" Property="Visibility" Value="Visible"/&gt; &lt;Setter TargetName="Expander" Property="Content" Value="-"/&gt; &lt;/Trigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;DockPanel&gt; &lt;TextBlock Text="{Binding Count, StringFormat='{}{0} Items'}" DockPanel.Dock="Top"/&gt; &lt;ItemsControl ItemsSource="{Binding}" Style="{StaticResource LogViewerStyle}"&gt; &lt;ItemsControl.Template&gt; &lt;ControlTemplate&gt; &lt;ScrollViewer CanContentScroll="True"&gt; &lt;ItemsPresenter/&gt; &lt;/ScrollViewer&gt; &lt;/ControlTemplate&gt; &lt;/ItemsControl.Template&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;VirtualizingStackPanel IsItemsHost="True"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;/ItemsControl&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre></li> </ul> <p>Code Behind: <em>(Notice that most of it is just boileplate to support the example (generate random entries)</em></p> <pre><code> public partial class LogViewer : Window { private string TestData = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; private List&lt;string&gt; words; private int maxword; private int index; public ObservableCollection&lt;LogEntry&gt; LogEntries { get; set; } public LogViewer() { InitializeComponent(); random = new Random(); words = TestData.Split(' ').ToList(); maxword = words.Count - 1; DataContext = LogEntries = new ObservableCollection&lt;LogEntry&gt;(); Enumerable.Range(0, 200000) .ToList() .ForEach(x =&gt; LogEntries.Add(GetRandomEntry())); Timer = new Timer(x =&gt; AddRandomEntry(), null, 1000, 10); } private System.Threading.Timer Timer; private System.Random random; private void AddRandomEntry() { Dispatcher.BeginInvoke((Action) (() =&gt; LogEntries.Add(GetRandomEntry()))); } private LogEntry GetRandomEntry() { if (random.Next(1,10) &gt; 1) { return new LogEntry() { Index = index++, DateTime = DateTime.Now, Message = string.Join(" ", Enumerable.Range(5, random.Next(10, 50)) .Select(x =&gt; words[random.Next(0, maxword)])), }; } return new CollapsibleLogEntry() { Index = index++, DateTime = DateTime.Now, Message = string.Join(" ", Enumerable.Range(5, random.Next(10, 50)) .Select(x =&gt; words[random.Next(0, maxword)])), Contents = Enumerable.Range(5, random.Next(5, 10)) .Select(i =&gt; GetRandomEntry()) .ToList() }; } } </code></pre> <p>Data Items:</p> <pre><code>public class LogEntry: PropertyChangedBase { public DateTime DateTime { get; set; } public int Index { get; set; } public string Message { get; set; } } public class CollapsibleLogEntry: LogEntry { public List&lt;LogEntry&gt; Contents { get; set; } } </code></pre> <p>PropertyChangedBase: </p> <pre><code> public class PropertyChangedBase:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { Application.Current.Dispatcher.BeginInvoke((Action) (() =&gt; { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); })); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload