在隐藏和禁用之间切换VerticalScrollBarVisibility时,ScrollViewer高度会更改

时间:2014-03-07 21:13:32

标签: .net wpf xaml

我有ScrollViewer这样的设置:

  <ScrollViewer x:Name="scroller"
                          Focusable="False"
                          HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Hidden">

当我将VerticalScrollBarVisibility从Hidden更改为Disabled时,ScrollViewer的高度从115变为107(使用Snoop确认)。

我什么也没改变。我很难过。我似乎无法弄清楚为什么会发生这种情况。

我试图在Kaxaml中重现它,但它不会发生。

仅供参考:我的具体例子是一个相当复杂的棱镜应用程序。

为什么VerticalScrollBarVisibility会导致ScrollViewer的高度缩小? (并切断控件的底部。)

更新

在Chris W.建议之前我没有想到它,但这可能与我的App Wide ScrollViewer风格有关。我要测试一下,看看我是否可以用它来重复。在这里,如果比我更聪明的头脑可以更快地找到问题。 (这很长,但需要制作自定义外观:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
                    xmlns:System="clr-namespace:System;assembly=mscorlib">

    <Brush x:Key="ScrollBackground">#7F5F5F5F</Brush>
    <Brush x:Key="ScrollForeground">#DADADA</Brush>
    <Brush x:Key="ScrollDisabled">#E3E3E3</Brush>
    <Color x:Key="ScrollHashMarksDark">#656565</Color>
    <Color x:Key="ScrollHashMarksLight">#C2C2C2</Color>
    <System:Double x:Key="HashSize">1</System:Double>

    <Style TargetType="{x:Type ScrollViewer}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Rectangle x:Name="Corner" Grid.Column="1" 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}" Margin="{TemplateBinding Padding}" />
                        <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" Width="8"/>
                        <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}" Height="8"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="HorizontalThumb" TargetType="{x:Type Thumb}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="4" />
                            <ColumnDefinition Width="4" />
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition />
                            <ColumnDefinition Width="4" />
                            <ColumnDefinition Width="4" />
                        </Grid.ColumnDefinitions>

                        <Ellipse Grid.Column="0" Grid.ColumnSpan="2" Fill="{StaticResource ScrollForeground}" />
                        <Rectangle Grid.Column="1" Grid.ColumnSpan="5" Fill="{StaticResource ScrollForeground}" />

                        <!--Add in the hash bars (in the middle of the scrollbar)-->
                        <StackPanel Orientation="Horizontal" Grid.Column="3">
                            <Border Width="{StaticResource HashSize}" Margin="0,0,2,0" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <Border Width="{StaticResource HashSize}" Margin="0,0,2,0" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <Border Width="{StaticResource HashSize}" Margin="0" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>



                        </StackPanel>
                        <Ellipse Grid.Column="5" Grid.ColumnSpan="2" Fill="{StaticResource ScrollForeground}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="VerticalThumb" TargetType="{x:Type Thumb}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="4" />
                            <RowDefinition Height="4" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="4" />
                            <RowDefinition Height="4" />
                        </Grid.RowDefinitions>
                        <Ellipse Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource ScrollForeground}" />
                        <Rectangle Grid.Row="1" Grid.RowSpan="5" Fill="{StaticResource ScrollForeground}" />
                        <!--Add in the hash bars (in the middle of the scrollbar)-->
                        <StackPanel Orientation="Vertical" Grid.Row="3">
                            <Border Height="{StaticResource HashSize}" Margin="0,0,0,2" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <Border Height="{StaticResource HashSize}" Margin="0,0,0,2" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <Border Height="{StaticResource HashSize}" Margin="0" CornerRadius="30">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Offset="0" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset=".75" Color="{StaticResource ScrollHashMarksDark}"/>
                                        <GradientStop Offset="1"  Color="{StaticResource ScrollHashMarksLight}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                        </StackPanel>
                        <Ellipse Grid.Row="5" Grid.RowSpan="2" Fill="{StaticResource ScrollForeground}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



    <Style TargetType="{x:Type ScrollBar}">
        <Style.Resources>
            <Style x:Key="ScrollBarGrid" TargetType="{x:Type Grid}">
                <Setter Property="Background" Value="{StaticResource ScrollBackground}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollBar}}, Path=IsEnabled}" Value="False">
                        <Setter Property="Background" Value="{StaticResource ScrollDisabled}"/>
                    </DataTrigger>
                </Style.Triggers>
             </Style>
        </Style.Resources>
        <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Width" Value="10"/>
        <Setter Property="MinWidth" Value="10"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Grid x:Name="Bg" SnapsToDevicePixels="true" Style="{StaticResource ScrollBarGrid}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.00001*"/>
                        </Grid.RowDefinitions>
                        <Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="1">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource ScrollBarPageButton}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource ScrollBarPageButton}"/>
                            </Track.IncreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb Style="{DynamicResource VerticalThumb}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="VerticalGripper" Width="8"/>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Orientation" Value="Horizontal">
                <Setter Property="Width" Value="Auto"/>
                <Setter Property="MinWidth" Value="0"/>
                <Setter Property="Height" Value="10"/>
                <Setter Property="MinHeight" Value="10"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ScrollBar}">
                            <Grid x:Name="Bg" SnapsToDevicePixels="true" Style="{StaticResource ScrollBarGrid}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.00001*"/>
                                </Grid.ColumnDefinitions>
                                <Track x:Name="PART_Track" Grid.Column="1" IsEnabled="{TemplateBinding IsMouseOver}">
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource ScrollBarPageButton}"/>
                                    </Track.DecreaseRepeatButton>
                                    <Track.IncreaseRepeatButton>
                                        <RepeatButton Command="{x:Static ScrollBar.PageRightCommand}" Style="{StaticResource ScrollBarPageButton}"/>
                                    </Track.IncreaseRepeatButton>
                                    <Track.Thumb>
                                        <Thumb Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="HorizontalGripper" Style="{DynamicResource HorizontalThumb}" Height="8"/>
                                    </Track.Thumb>
                                </Track>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

0 个答案:

没有答案