我有一些问题,我将感谢任何人的帮助!
我有一个动态计数列表框的窗口。使用MVVM。对于列表框及其功能,我们创建了一个带有viewModel的用户控件。因此,当创建窗口时,我在窗口的构造函数中创建了特定的userControls计数。真正的问题是。列表框中有it's MaxWidth=350。并且每个listboxItem最后都带有按钮样式。当名称很短时-一切正常,用户可以在窗口加载后立即看到列表框中的按钮。但是如果名称太长,由于MaxWidth=350 (预期行为),listBox会创建一个水平scrollBar。因此,一旦加载了表单,看起来按钮并不是在某些listBoxes (参见image2:)Image2: scrollBar appeared and buttons are not visible中创建的,所以问题是:如何在窗口显示之前以编程方式移动滚动条?图3显示了我需要的内容,图2显示了列表框的当前视图。Needed result after loading the window你知道怎么把scrollBar移到最后吗?
发布于 2016-01-25 20:57:55
感谢您,@Yog,感谢您在Focus()上的提示。我确实没有意识到Focus方法可以移动scrollBar!我记得我已经在c#中使用过的方法,我在这个站点中找到了这个方法:How can I find WPF controls by name or type?,所以我只是将这个方法重写为vb.net作为扩展方法,在ListBox上调用它,找到了带有按钮的stackPanel和stackPanel!重要信息:如果有人想解决他的Focus()方法的问题,请确保您的特定xaml元素已经设置了Focus
下面是我在Vb.NET上的方法:
Public Function FindChild(Of T As DependencyObject)(parent As DependencyObject, childName As String) As T
If parent Is Nothing Then Return Nothing
Dim foundChild As T = Nothing
Dim childrenCount As Integer = VisualTreeHelper.GetChildrenCount(parent)
For i = 0 To childrenCount
Dim child As DependencyObject
Try
child = VisualTreeHelper.GetChild(parent, i)
Catch ex As Exception
Continue For
End Try
'If the child is not of the request child type child
Dim childType As T = TryCast(child, T)
If childType Is Nothing Then
'recursively drill down the tree
foundChild = child.FindChild(Of T)(childName)
'If the child is found, break so we do not overwrite the found child.
If foundChild IsNot Nothing Then Exit For
Else
If Not String.IsNullOrEmpty(childName) Then
Dim frameworkElement = TryCast(child, FrameworkElement)
'If the child's name is set for search
If frameworkElement IsNot Nothing AndAlso frameworkElement.Name = childName Then
'if the child's name is of the request name
foundChild = TryCast(child, T)
Exit For
End If
Else
'child element found.
foundChild = TryCast(child, T)
Exit For
End If
End If
Next
Return foundChild
End FunctionVisualTreeHelper.GetChild方法抛出了一个异常,我不知道为什么,所以我就把它放到了try-catch中。
https://stackoverflow.com/questions/34986842
复制相似问题