【发布时间】:2017-07-12 10:30:19
【问题描述】:
我制作了以下简单控件,我将其作为弹出窗口添加到另一个窗口(开始隐藏,单击按钮后变为可见)
<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 调暗为字符串 将 _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
结束类
如果我替换了 loadGroups() 对于 gsList 中的每个 grp 作为字符串 grouplistBox.Items.Add(grp) 下一个
使用注释掉的代码,滚动条可以完美运行。 使用 gsList 中的字符串列表,我得到了奇怪的行为。
我可以发布整个项目,但由于它是针对我们的活动目录结构进行硬编码的,因此无法在其他系统上编译。 (另外,因为我只是用它来学习 WPF 并且它是一个内部工具,我已经硬编码了许多密码)
【问题讨论】:
-
好的,我已经解决了这个问题。答案更令人困惑。事实证明,在代码中,我将组的 DisplayName 添加到字符串列表中,它为每个组返回“Nothing”。然而以某种方式正确地显示了列表(尽管有一个扭曲的滚动条)。现在我已将其更改为返回 grp.name 而不是 displayname,并且一切都按我的预期工作。是我,还是 WPF 有可怕的错误?
标签: wpf wpf-controls