ListBox中的ScrollViewer无法正常工作。 WPF

时间:2010-05-11 14:19:57

标签: wpf listbox scrollviewer

我在我的控制中定义了以下内容:

<Style TargetType="primitives:CalendarDayButton" x:Key="EventCalendarDayButton">
        <Setter Property="Background" Value="#FFBADDE9"/>
        <Setter Property="MinWidth" Value="5"/>
        <Setter Property="MinHeight" Value="5"/>
        <Setter Property="FontSize">
            <Setter.Value>
                <Binding Path="DayFontSize">
                    <Binding.RelativeSource>
                        <RelativeSource Mode="FindAncestor" AncestorType="{x:Type toolkit:Calendar}" />
                    </Binding.RelativeSource>
                </Binding>
            </Setter.Value>
        </Setter>
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="primitives:CalendarDayButton">
                    <Grid MouseDown="Grid_MouseDown">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="18" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>                         
                        <Rectangle x:Name="SelectedBackground" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                        <Rectangle x:Name="Background" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}" />
                        <Rectangle x:Name="InactiveBackground" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="#FFA5BFE1"/>
                        <Border>
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop x:Name="StartGradient" Color="#FFD5E2F2" Offset="0"/>
                                    <GradientStop x:Name="EndGradient" Color="#FFB9C9DD" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <ContentPresenter x:Name="NormalText" Margin="5,1,5,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                <TextElement.Foreground>
                                    <SolidColorBrush x:Name="selectedText" Color="#FF333333" />
                                </TextElement.Foreground>
                            </ContentPresenter>
                        </Border>
                        <Rectangle x:Name="Border" StrokeThickness="0.5" Grid.RowSpan="2" SnapsToDevicePixels="True">
                            <Rectangle.Stroke>
                                <SolidColorBrush x:Name="BorderBrush" Color="#FF5D8CC9"/>
                            </Rectangle.Stroke>
                        </Rectangle>
                        <Path x:Name="Blackout" Grid.Row="1" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"/>
                        <Rectangle Width="0" x:Name="DayButtonFocusVisual" Grid.Row="1" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1" Stroke="#FF45D6FA"/>                            
                        <Button x:Name="ActivateDayViewOnDay" Grid.Row="0" Opacity="0.3" Height="15" Margin="1,1,1,1" PreviewMouseLeftButtonDown="DayView_Click" />                          
                        <ScrollViewer Grid.Row="1" >
                            <ScrollViewer.Content>                          
                            <local:TestListBox
                            x:Name="eventsLbx" 
                            Background="Transparent"
                            BorderBrush="Transparent"
                            >
                            <local:TestListBox.ItemsSource>
                                <MultiBinding Converter="{StaticResource calendarEventsConverter}">
                                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EventCalendar}}" Path="CalendarEvents"/>
                                    <Binding RelativeSource="{RelativeSource Mode=Self}" Path="DataContext"/>
                                </MultiBinding>
                            </local:TestListBox.ItemsSource>
                            <local:TestListBox.ItemTemplate>
                                <DataTemplate>                              
                                        <TextBlock TextTrimming="CharacterEllipsis" HorizontalAlignment="Center" FontSize="12" Text="{Binding Text}" />                             
                                </DataTemplate>
                            </local:TestListBox.ItemTemplate>
                        </local:TestListBox>   
                           </ScrollViewer.Content>
                        </ScrollViewer>
                    </Grid>                            
                    <ControlTemplate.Triggers>
                        <Trigger SourceName="eventsLbx" Property="HasItems" Value="False">
                            <Setter TargetName="eventsLbx" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在,如果有多个项目可见,则滚动查看器会正确显示,但用户无法拖动滚动查看器中间按钮进行滚动。

用户可以单击滚动查看器末尾的箭头进行滚动,但是他无法单击滚动条上显示的栏并拖动它以实际滚动内容。

我无法弄清楚为什么会这样......

2 个答案:

答案 0 :(得分:1)

如果要滚动ListBox中的项目,则无需将其包裹在ScrollViewer中。 ListBox原生支持滚动。这意味着,如果ListBox太小而无法显示其所有项目,则会自动将ScrollBar添加到旁边。

答案 1 :(得分:0)

您的ScrollViewer和ListBox位于Grid中,并且您在该Grid的MouseDown事件后面有一些代码。

<Grid MouseDown="Grid_MouseDown">

你在该方法中所做的事情可能会妨碍ScrollViewer的鼠标机制,破坏事件冒泡或类似的事情。

检查您的代码隐藏,或在此处发布,以便我们为您提供帮助:)