簡體   English   中英

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

[英]ScrollViewer in a ListBox not working. WPF

我在控件中定義了以下內容:

<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>

現在,如果有多於可見的項目,則滾動查看器會正確顯示,但用戶無法拖動滾動查看器中間按鈕進行滾動。

用戶可以單擊滾動查看器末尾的箭頭進行滾動,但不能單擊滾動條上出現的欄並將其拖動以實際滾動內容。

我不知道為什么會這樣...

如果要滾動ListBox的項目,則無需將其包裝在ScrollViewer ListBox本機支持滾動。 這意味着,如果您的ListBox太小而無法顯示其所有項目,它將自動在側面添加ScrollBar

您的ScrollViewer和ListBox都位於網格內,並且該網格的MouseDown事件背后有一些代碼。

<Grid MouseDown="Grid_MouseDown">

您在該方法中所做的事情可能會妨礙ScrollViewer的鼠標機制,破壞事件冒泡或類似的事件。

檢查您的背后代碼,或將其發布在這里,以便我們為您提供幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM