如何使隐式滚动查看器出现在左侧而不是右侧

时间:2012-03-15 15:33:59

标签: wpf xaml

当列表中有足够的对象时,我有一个ScrollViewer出现在右侧。如何让它出现在左侧?

<ListBox 
x:Name="MessageListBox" 
BorderThickness="0" 
ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
HorizontalContentAlignment="Stretch"  
AlternationCount="2" 
ItemContainerStyle="{StaticResource AltStyle}" 
SelectionMode="Extended" 
>
 <ListBox.ItemTemplate >
  <DataTemplate>
   <!-- button --> 
<!-- closing tags --> 

4 个答案:

答案 0 :(得分:9)

可以通过将列表包装到ScrollViewer并将ScrollViewer属性FlowDirection更改为&#34; RightToLeft&#34;来实现。也不要忘记恢复列表&#39; FlowDirection到LeftToRight,否则它将继承父方向。

<ScrollViewer FlowDirection="RightToLeft"
    CanContentScroll="False" VerticalScrollBarVisibility="Auto">
  <ListBox ItemsSource="{Binding CustomItems}" FlowDirection="LeftToRight"/>
</ScrollViewer>

我在MSDN社交博客上找到了它, http://social.msdn.microsoft.com/Forums/vstudio/en-US/e796231d-5c92-44b4-bb7e-c3b74d81a99c/how-to-set-verticalscroll-bar-on-left-side?forum=wpf

答案 1 :(得分:3)

这是通过修改ListBox的ScrollViewer的模板来实现的。

首先修改Container Grid的ColumnDefinitions。然后,把东西放到正确的列中。

玩得开心!

<!--This should be able to be placed on any WPF Window for testing purposes-->
<ListBox Height="85" VerticalAlignment="Top" Margin="117,110,300,0">
    <ListBox.Template>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
                <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                    <ScrollViewer.Template>
                        <ControlTemplate TargetType="{x:Type ScrollViewer}">
                            <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>                    
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Rectangle x:Name="Corner" Grid.Column="0" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
                                <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                            </Grid>
                        </ControlTemplate>
                    </ScrollViewer.Template>
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsGrouping" Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ListBox.Template>
    <TextBox Text="Hi Mom!"/>
    <TextBox Text="Hi Dad!"/>
    <TextBox Text="Hi Father!"/>
    <TextBox Text="Hi Mother!"/>
    <TextBox Text="Hi Padre!"/>            
</ListBox>

P.S。如果你想移动Horizo​​ntalScrollBar,只需重新排序RowDefinitions,做同样的练习(把每个子组件放在正确的行中)

答案 2 :(得分:1)

我的解决方案是为RightToLeft设置FlowDirection ListView,然后为LeftToRight的孩子ItemsPanelTemplate设置StackPanel

<ListView FlowDirection="RightToLeft" ItemsSource="{Binding SomeDataSource}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel FlowDirection="LeftToRight"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

原始ListView显示在右侧,ListView的滚动条位于左侧(通过上面的源代码)-左侧显示。

Original List View shown on the right and List View with scrollbar positioned on the left (by source code above) - shown on the left.

答案 3 :(得分:0)

只需将ListBox的嵌入式scrollviewer设置为RightToLeft:

<ListBox ScrollViewer.FlowDirection="RightToLeft" />