我有一个Expander带有嵌套ScrollViewer,如下所示:

代码(简体版)

<Expander.Content>
    <ScrollViewer VerticalScrollBarVisibility="Auto" >
        <StackPanel Orientation="Vertical">
            <TextBox FontSize="16"
                    BorderThickness="0"
                    IsReadOnly="True"
                    Background="Transparent"
                    Foreground="MidnightBlue"
                    TextWrapping="Wrap"
                    Text="{Binding LoggingMessage, Mode=OneWay}">
                </TextBox>
            /StackPanel>
         </ScrollViewer>
    </Expander.Content>
</Expander>


我需要更改ScrollViewer的一侧,因此其显示在左侧。

最简单的解决方案是什么?

最佳答案

您可以自定义滚动查看器的模板以更改滚动条的位置(除其他外)。模板自定义的MSDN示例实际上显示了如何将垂直滚动条向左移动。

http://msdn.microsoft.com/en-gb/library/aa970847(v=vs.85).aspx

这是为方便起见的代码:

<Style x:Key="LeftScrollViewer" TargetType="{x:Type ScrollViewer}">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <ScrollContentPresenter Grid.Column="1"/>

                    <ScrollBar Name="PART_VerticalScrollBar"
                        Value="{TemplateBinding VerticalOffset}"
                        Maximum="{TemplateBinding ScrollableHeight}"
                        ViewportSize="{TemplateBinding ViewportHeight}"
                        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                    <ScrollBar Name="PART_HorizontalScrollBar"
                        Orientation="Horizontal"
                        Grid.Row="1"
                        Grid.Column="1"
                        Value="{TemplateBinding HorizontalOffset}"
                        Maximum="{TemplateBinding ScrollableWidth}"
                        ViewportSize="{TemplateBinding ViewportWidth}"
                        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于c# - WPF ScrollViewer端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15430343/

10-13 07:57