我已經做了以下簡單的控制,這是我添加到另一個窗口的彈出(開始隱藏,被改爲可見點擊一個按鈕)奇滾動條的行爲
<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
除了列表框旁邊的滾動條,一切似乎都正常工作。這從大約整個控件的一半高度開始,當試圖將其向下滑動時,列表不會滾動,但滾動器的大小會縮小,直到到達一個小欄,然後開始滾動到窗口的內容。同樣,將條拖回:當條位於滾動區域一半左右時,列表框將進入列表頂部,然後條會展開。
任何人都可以幫助我糾正這種行爲/解釋我做錯了什麼?
謝謝。 皮特。
經過試驗,事情越來越離奇....
進口AshbyTools 進口System.DirectoryServices.AccountManagement 進口System.ComponentModel
公共類AddGroupsCtrl 暗淡_domainString As String Dim _ouString As String
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
末級
在loadGroups()如果我更換 對於每個GRP作爲字符串在gsList grouplistBox.Items.Add(GRP) 接着
與註釋代碼,滾動條完美的作品。 有了gsList的字符串列表,我得到了奇怪的行爲。
我可以發佈整個項目,但由於它是硬編碼爲我們的活動目錄結構,它不會在另一個系統上編譯。 (另外,因爲我只是用這個來學習WPF,它是一個內部工具,我已經硬編碼了很多密碼)
好吧,我已經解決了這個問題。答案更令人困惑。 原來,在代碼中,我得到了要添加到字符串列表的組的DisplayName,它爲每個字符都返回'Nothing'。然而以某種方式正確地顯示了列表(儘管用一個棘手的滾動條)。 現在我已經改變它返回grp.name而不是displayname,並且所有事情都像我期望的那樣工作。 是我還是WPF可怕的越野車? – Peterp