wpf 更改列表框中的滚动查看器模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16683477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change scrollviewer template in listbox
提问by Ka Boom
I'm a newbie with WPF. I want to change the template of scroll viewer in listbox. I found an Apple style scroll bar in this blog
我是 WPF 的新手。我想更改列表框中滚动查看器的模板。我在这个博客中发现了一个苹果风格的滚动条
But i dont know how to apply that scroll viewer template to listbox. Can anyone help me?
Here is the XAMLcode of Apple scroll viewer template:
但我不知道如何将该滚动查看器模板应用于列表框。谁能帮我?这是XAMLApple 滚动查看器模板的代码:
<SolidColorBrush x:Key="ScrollBarDisabledBackground" Color="#F4F4F4"/>
<Style x:Key="VerticalScrollBarPageButton" 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="ScrollBarThumb" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border Background="#FF868686" BorderThickness="0,0,1,0" Height="Auto" CornerRadius="4" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HorizontalScrollStyle" TargetType="{x:Type ScrollBar}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg" Background="{TemplateBinding Background}" Height="10" SnapsToDevicePixels="true">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}">
<Track.DecreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{StaticResource ScrollBarThumb}" Cursor="Hand"/>
</Track.Thumb>
</Track>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bg" Value="{StaticResource ScrollBarDisabledBackground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="AppleStyleVerticalScrollBarStyle" TargetType="{x:Type ScrollBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg" SnapsToDevicePixels="true" Width="10" HorizontalAlignment="Right" Margin="0">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}">
<Track.DecreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButton}" />
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{DynamicResource ScrollBarThumb}" Cursor="Hand"/>
</Track.Thumb>
</Track>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="AppleStyleScrollBarStyle" TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle x:Name="Corner" Grid.Column="1" Fill="{x:Null}" Grid.Row="1"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}"
CanHorizontallyScroll="False" CanVerticallyScroll="False"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}" Grid.Column="0"
Margin="{TemplateBinding Padding}" Grid.Row="0"/>
<ScrollBar x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1"
Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0"
Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}" Style="{DynamicResource AppleStyleVerticalScrollBarStyle}"
Background="{x:Null}" Width="Auto" Margin="0"/>
<ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0"
Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1"
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportWidth}" Style="{DynamicResource HorizontalScrollStyle}"/>
</Grid>
</ControlTemplate>
回答by dkozl
ScrollViewer, like few other controls in WPF, have some named parts. In order to be used in correct places they need to be named properly. ScrollViewerstyling has been explained on this MSDNsite but basically it has 3 parts ScrollContentPresenter, HorizontalScrollBarand VerticalScrollBar. In turn each ScrollBarwill have its own named parts to style. And for that go to this MSDNsite. In your case you can wrap your ListBoxin ScrollViewerlike this:
ScrollViewer与 WPF 中的其他几个控件一样,有一些命名部分。为了在正确的地方使用它们,它们需要正确命名。ScrollViewer样式已在此MSDN站点上进行了解释,但基本上它包含 3 个部分ScrollContentPresenter,HorizontalScrollBar以及VerticalScrollBar. 反过来,每个ScrollBar人都有自己的命名部分来设计风格。为此,请访问此MSDN站点。在你的情况下,你可以用你ListBox在ScrollViewer这样的:
<ScrollViewer>
<ListBox/>
</ScrollViewer>
In this case you might also consider hiding original scroll bars of your ListBox. If you don't put any restriction on Heightof Listboxit will grow to fit all elements and this in turn will cause your ScrollViewerto show your custom scroll bars.
Or you can change template of your ListBoxlike this for example (MSDN):
在这种情况下,您还可以考虑隐藏ListBox. 如果您不对其施加任何限制Height,Listbox它将增长以适应所有元素,这反过来会导致您ScrollViewer显示自定义滚动条。或者您可以更改您的模板,ListBox例如(MSDN):
<Style TargetType={x:Type ListBox}>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<ScrollViewer x:Name="ScrollViewer">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and then customize your ScrollViewerthere
然后在ScrollViewer那里定制你的

