Silverlight - 用于列表框的样式ScrollViewer

时间:2012-01-18 11:49:08

标签: c# wpf silverlight

所以我一直在尝试设置滚动查看器的样式,以便我可以更改滚动条的外观。但我面临的问题是,当我将scrollviewer样式应用于ListBox时,我再也看不到内容了。

这是我现有的风格:(目前默认不变)

<Style x:Key="CustomScrollViewerStyle" TargetType="ScrollViewer">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
    <Setter Property="Padding" Value="4"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2">
                    <Grid Background="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <ScrollContentPresenter x:Name="ScrollContentPresenter" Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}"/>
                        <Rectangle Grid.Column="1" Fill="#FFE9EEF4" Grid.Row="1"/>
                        <ScrollBar x:Name="VerticalScrollBar" Grid.Column="1" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Margin="0,-1,-1,-1" Minimum="0" Orientation="Vertical" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{TemplateBinding VerticalOffset}" ViewportSize="{TemplateBinding ViewportHeight}" Width="18"/>
                        <ScrollBar x:Name="HorizontalScrollBar" Grid.Column="0" Height="18" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Margin="-1,0,-1,-1" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{TemplateBinding HorizontalOffset}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的实际ListBox xaml是:

<Grid x:Name="LayoutRoot" Background="Transparent" Height="250">
    <ListBox x:Name="iListBox" DataContext="{Binding}" ItemsSource="{Binding Path=ListVM.MyCollection}" 
             BorderBrush="Transparent" Background="Transparent" 
             ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
        <ListBox.Template>
            <ControlTemplate TargetType="ItemsControl">
                <ScrollViewer Style="{StaticResource CustomScrollViewerStyle}"/>
            </ControlTemplate>
        </ListBox.Template>
        <ListBox.ItemTemplate >
            <DataTemplate >
                <Grid Background="Transparent" Loaded="Grid_Loaded">
                    <IReviewerList1:MyCollectionDataItem />
                </Grid> 
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

目前,我看到的只是一个滚动条而且没有dataItems。想知道我做错了什么?任何帮助都会很棒。

谢谢

1 个答案:

答案 0 :(得分:3)

问题是ListBox不知道在哪里注入它的Items。如果您在样式ItemsPresenter中插入ScrollViewer,则项目应再次可见。

<ListBox.Template>
    <ControlTemplate TargetType="ItemsControl">
        <ScrollViewer Style="{StaticResource CustomScrollViewerStyle}">
            <!-- I'm not sure if the correct name is necessary, or if
                 it's just for applying visual states -->
            <ItemsPresenter x:Name="itemsPresenter" />
        </ScrollViewer>
    </ControlTemplate>
</ListBox.Template>