WPF 控件中的奇怪滚动条行为

Odd Scrollbar behaviour in WPF control

我制作了以下简单控件,我将其作为弹出窗口添加到另一个 window(开始时隐藏,单击按钮后变为可见)

<UserControl x:Class="AddGroupsCtrl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ASManager2017"
         mc:Ignorable="d" d:DesignWidth="255" Height="413">

<GroupBox x:Name="groupBox" Header=" Active Directory Groups" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Height="350" Width="233" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}">
    <Grid Height="auto">

        <ListBox x:Name="grouplistBox" HorizontalAlignment="Stretch" Height="auto" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="auto" SelectionMode="Extended" ScrollViewer.VerticalScrollBarVisibility="Visible" Background="{DynamicResource fadeBrush}"/>
        <Button x:Name="addButton" Content="Add" HorizontalAlignment="Right" Margin="0,0,22,0" VerticalAlignment="Bottom" Width="75"/>
        <Button x:Name="button" Content="X" HorizontalAlignment="Right" Margin="0,0,22,0" VerticalAlignment="Top" Width="20" Height="20" Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
    </Grid>
</GroupBox>

在这背后的代码中,我有以下片段....

    For Each grp As String In gsList
        grouplistBox.Items.Add(grp)
    Next

除了列表框旁边的滚动条外,一切似乎都正常。这开始于整个控件高度的一半左右,当尝试将其向下滑动时,列表不会滚动,但滚动条会缩小大小直到到达一个小条,然后开始滚动到 [= 的内容50=]。同样向上拖动栏:当栏大约在滚动区域的一半时,列表框将到达列表的顶部,然后栏将展开。

谁能帮我改正这个behaviour/explain我做错了什么?

谢谢。 皮特

经过更多的实验,事情变得越来越奇怪....

导入 AshbyTools 进口 System.DirectoryServices.AccountManagement 进口 System.ComponentModel

Public Class AddGroupsCtrl 将 _domainString 变暗为字符串 将 _ouString 变暗为字符串

Public Event addClicked(ByVal glist As List(Of String))

<Description("ouString"), DisplayName("OU String"), Category("Data")>
Public Property ouString As String
    Get
        Return _ouString
    End Get
    Set(value As String)
        _ouString = value
    End Set
End Property

<Description("domainString"), DisplayName("Domain String"), Category("Data")>
Public Property domainString As String
    Get
        Return _domainString
    End Get
    Set(value As String)
        _domainString = value
    End Set
End Property
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
    Me.Visibility = Visibility.Hidden
End Sub

Public Sub loadGroups()
    grouplistBox.Items.Clear()
    Dim groupCTX As PrincipalContext = ADTools.getConnection(domainString, ouString)
    Dim gList As List(Of GroupPrincipal) = ADTools.getManagedGroups(groupCTX)
    Dim gsList As New List(Of String)
    For Each grp As GroupPrincipal In gList
        If Not (grp.DistinguishedName.Contains("Staff Groups") Or grp.DistinguishedName.Contains("Subject Groups") Or grp.DistinguishedName.Contains("Tutor Groups")) Then
            gsList.Add(grp.DisplayName)
        End If
    Next

    'For n As Integer = 1 To 70
    '    grouplistBox.Items.Add("item" & n)
    'Next

    gsList.Sort()
    For Each grp As String In gsList
        grouplistBox.Items.Add(grp)
    Next
End Sub

Private Sub addButton_Click(sender As Object, e As RoutedEventArgs) Handles addButton.Click
    Dim ret As New List(Of String)
    For Each grp In grouplistBox.SelectedItems
        ret.Add(grp.ToString)
    Next
    RaiseEvent addClicked(ret)
End Sub

结束Class

如果我更换 loadGroups() 对于 gsList 中的每个 grp 作为字符串 grouplistBox.Items.Add(组) 下一个

使用注释掉的代码,滚动条完美运行。 使用 gsList 中的字符串列表,我得到了奇怪的行为。

我可以 post 整个项目,但由于它是针对我们的活动目录结构进行硬编码的,因此无法在其他系统上编译。 (另外,因为我只是用它来学习 WPF 并且它是一个内部工具,所以我硬编码了许多密码)

好的,我已经解决了这个问题。而答案更加扑朔迷离。事实证明,在代码中,我将组的 DisplayName 添加到字符串列表中,每个组都是 returning 'Nothing'。然而以某种方式正确显示了列表(尽管有一个扭曲的滚动条)。现在我将其更改为 return grp.name 而不是显示名称,一切都如我所料。