滚动ListBox,包装TextBlock

时间:2013-08-01 12:23:27

标签: wpf

我正在尝试使用WPF替换现有的Winforms项目。让我们将这篇文章称为“日志查看器”。每个日志条目都包含一个标题和一些正文。查看器应在一个长滚动列表中显示这些内容。

原始(Winforms)logviewer在日志条目数量及其大小较小时运行良好,但在显示大量长条目时遇到一些问题; WPF virtualizingstackpanel解决了这些问题,但WPF,或者我缺乏经验,正在增加一些问题。

如果我围绕ListBox构建logviewer窗口,除了滚动是按项目而不是按像素平滑滚动这一事实外,它的工作原理非常完美。我认为这可以通过转移到.Net 4.5来解决,但这不是一个简单的选择。

或者,如果我围绕TreeView构建它,它会完美滚动但文本不会换行;相反,它每段形成一个长行。

这是treeview版本的xaml;它从this SO question大量借用,其中样式似乎用于纠正非包装问题。它滚动得很漂亮,但它仍然没有包裹。

<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>

我也尝试过基于this SO answer的xaml,但我无法让它工作 - 它仍然按项目滚动。

任何帮助都非常感激。

0 个答案:

没有答案