0

I am attempting to replace a piece of an existing Winforms project using WPF. Let's call this piece a 'log viewer'. Each log entry consists of a header and some body text. The viewer should display these in one long scrollable list.

The original (Winforms) logviewer works well when the number of log entries, and their size, is small, but it suffers from some problems when displaying large numbers of long entries; the WPF virtualizingstackpanel solves these problems, but WPF, or my lack of experience with it, is adding some problems of its own.

If I build the logviewer window around a ListBox, it works perfectly except for the fact that scrolling is by item rather than smooth scrolling 'by pixel'. I gather this can be fixed by moving to .Net 4.5 but that's not a simple option.

Alternatively if I build it around a TreeView, it scrolls perfectly but the text does not wrap; instead it forms one long line per paragraph.

Here's the xaml for the treeview version; it borrows heavily from this SO question where the style seems to be used to correct the non-wrapping issue. It scrolls beautifully, but it still doesn't wrap.

<Window x:Class="zCasesheet.wCasesheet"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"

    Title="MainWindow" Height="440" Width="674">

<Window.Resources>
    <Style x:Key="MyTreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <Border Name="myBorder" 
                    SnapsToDevicePixels="true" 
                    CornerRadius="0,0,0,0" 
                    VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch"
                    BorderThickness="0"
                    BorderBrush="Transparent"
                    Height="Auto"
                    Margin="1,1,1,3" 
                    Background="Transparent">
                        <ContentPresenter Grid.Column="1" x:Name="PART_Header" HorizontalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid Name="grCasesheet" Background="#FFF6E7C9">
    <Grid.RowDefinitions>
        <RowDefinition Height="356*" />
        <RowDefinition Height="45" />
    </Grid.RowDefinitions>
    <TreeView  HorizontalAlignment="Stretch" Margin="2,2,2,0" VerticalAlignment="Top" Name="lstNarratives"    ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.CanContentScroll="True"  >

        <TreeView.ItemTemplate >

            <DataTemplate >

                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="35" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>


                    <TextBox Grid.Row="0"  Background="Honeydew" HorizontalAlignment="Stretch" BorderBrush="Black" Text="{Binding Path=HeaderText, StringFormat=d,  ConverterCulture={x:Static gl:CultureInfo.CurrentCulture} }"/>
                    <TextBlock  Grid.Row="1" TextWrapping="Wrap" HorizontalAlignment="Stretch" Text="{Binding Path=BodyText}" Margin="10,10,10,10"/>

                </Grid>

            </DataTemplate >

        </TreeView.ItemTemplate >

    </TreeView>

</Grid>

I've also tried xaml based on this SO answer, but I can't get that to work either - it still scrolls by item.

Any help much appreciated.

Community
  • 1
  • 1
peterG
  • 1,651
  • 3
  • 14
  • 23
  • Might be relevant: http://stackoverflow.com/questions/8934934/how-to-make-text-wrap-in-a-wpf-treeviewitem/8935517#8935517 – H.B. Aug 01 '13 at 12:46
  • Thanks HB. That looks promising. It's right at the extreme end of my currently sketchy WPF expertise though. Could you maybe expand a little bit on " change the Grid.ColumnSpan to 2 on the Bd Border he mentioned in the ControlTemplate " ? – peterG Aug 01 '13 at 13:16
  • It's a property set on the `Border` with the name `Bd` in the **default template**. You overwrite the template with your own and do not have this border at all. – H.B. Aug 01 '13 at 13:54
  • changing the grid.columnspan to 2 gives me word-wrap but loses the virtualization. Removing the border altogether removes the content. – peterG Aug 02 '13 at 02:37

0 Answers0