2

I have some issue and I'll appreciate any help on it!

I have a window with dynamic count of listboxes. MVVM was used. For listbox and it's functionality a usercontrol with viewModel was created. So when window is creating, I create my specific count of userControls in the constructor of the window. And actually the problem. Listbox has it's MaxWidth=350. And each listboxItem styled with buttons in the end. When names are short - everything OK and user can see buttons in the listbox exactly after window was loaded. But if names are too long, the listBox creates a horizontal scrollBar because of MaxWidth=350 (expected behavior.) So once the form is loaded, it seems like the buttons were not created in some listBoxes (see image2:)Image2: scrollBar appeared and buttons are not visible So the question is: how can I programmatically move scrollbar before the window will be shown? Image 3 shows what I need, and image 2 shows the current view of the listbox. Needed result after loading the window Do you know how to move scrollBar to the end?

desiderium
  • 41
  • 6
  • Try using `Focus()` function of button as `Button2.Focus()` at `Load` event of window(`Form`) – Yog Jan 25 '16 at 08:28
  • How can I focus on button, which is created in listBoxItem's style? @Yog – desiderium Jan 25 '16 at 09:09
  • you can see [here](http://stackoverflow.com/questions/2283143/how-can-i-set-the-focus-to-a-listbox-properly-on-load-if-it-uses-databinding?answertab=votes#tab-top) . I am not sure it will help you or not. – Yog Jan 25 '16 at 10:11

1 Answers1

1

Thank you, @Yog, for your tip with Focus(). I indeed didn't realize that Focus method can move scrollBar! And I remembered the method, that I had already used in c#, I found that method in this site: How can I find WPF controls by name or type? So I just rewrote this method to vb.net as an Extension method, called it on ListBox, found stackPanel with buttons and Focus() that stackPanel! IMPORTANT: if somebody wants to figure out his problem with Focus() method, make sure that your specific xaml element has set Focusable=true

Here is my method on 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 Function

VisualTreeHelper.GetChild method threw an exception, I didn't figure out why, so I just put it to try-catch.

Community
  • 1
  • 1
desiderium
  • 41
  • 6