Nir Hasson
Topic Author
Posts: 68
Joined: 10 Nov 2013, 21:20
Contact:

Get List Box Item

24 Dec 2013, 13:25

Hi,

I have a list box that uses an ItemTemplate for it's item representation and it's Item source is bounded to my custom DataContext .
How can I get access to an actual ListBoxItem ?
I saw the ListBox method that seems to do it -
ListBoxItem* ListBox::GetListBoxItemFromItem(Core::BaseComponent* item) const;
But it is a private method..

Thanks
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Get List Box Item

24 Dec 2013, 17:58

There is no public method available now to achieve this, but you can write some helper code to find the ListBoxItem container of your data item.
ListBoxItem* FindListBoxItem(ListBox* listBox, BaseComponent* item)
{
    // Assuming you named ItemsHost panel in your ListBox template
    // Otherwise, you have to find it iterating ListBox children with
    // VisualTreeHelper,and looking for a Panel with the IsItemsHost
    // property set to true
    Panel* itemsHost = NsStaticCast<Panel*>(
        listBox->GetTemplateChild("ItemsHostPanel"));

    // Search the requested list box item
    NsSize numItems = itemsHost->Count();
    for (NsSize i = 0; i < numItems; ++i)
    {
        BaseComponent* container = itemsHost->Get(i);
        ListBoxItem* lbi = NsStaticCast<ListBoxItem*>(container);

        // ListBoxItem contains the instantiation of data template visual tree
        FrameworkElement* element = NsStaticCast<FrameworkElement*>(
            lbi->GetContent());

        // Data item is set as DataContext of the DataTemplate root element
        if (element->GetDataContext() == item)
        {
            // Found!! :)
            return lbi;
        }
    }

    // Data item not found
    return 0;
}
 
Nir Hasson
Topic Author
Posts: 68
Joined: 10 Nov 2013, 21:20
Contact:

Re: Get List Box Item

26 Dec 2013, 16:50

Thanks a Lot !
Will try that as soon as I'll get over the rest of my port.
For now I used different approach to achieve the original goal which was to apply keyboard focus on certain list box item.
 
descala
Posts: 15
Joined: 19 Sep 2013, 20:11

Re: Get List Box Item

03 Apr 2014, 23:39

I'm trying the approach shown above, and I have managed to get the ListBoxItem*, but calling item->GetTemplateChild() always returns null.

When the UI is rendered, the template appears to be properly applied to the listbox items. What could be causing GetTemplateChild to return null?
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Get List Box Item

04 Apr 2014, 01:54

The GetTemplateChild(childName) function looks for an element in the VisualTree of the ControlTemplate with the specified name. So you have to set the x:Name property in the panel of your template that will be used to host the items. If you are using the default theme template, you won't be able to get the ItemsHost that way because the panel element does not have a name set.
 
descala
Posts: 15
Joined: 19 Sep 2013, 20:11

Re: Get List Box Item

04 Apr 2014, 05:49

Thanks. I traversed the visual tree and noticed that it wasn't yet being populated with the template elements (I was trying to access the template elements right after adding an item to the listbox). I decided to try waiting until the next frame, and this has gotten me further. A traversal of the tree (starting from the ListBoxItem) gets me the following:
Child type: Grid, name:
  Child type: Border, name: Focus
    Child type: Border, name: Border
      Child type: ContentPresenter, name:
        Child type: Grid, name:
          Child type: Rectangle, name:
            Child type: TextBox, name: ModelNameTextBox
              Child type: Grid, name:
                Child type: Border, name: Focus
                  Child type: Border, name: Border
                    Child type: ScrollViewer, name: PART_ContentHost
The element I'm trying to get at is ModelNameTextBox. I am trying item->GetTemplateChild("ModelNameTextBox").

I have tried several combinations of FindName and GetTemplateChild, calling those from elements at various points in the hierarchy, but both always seem to return null. What could I be doing wrong?
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Get List Box Item

07 Apr 2014, 18:11

I see you are trying to find a named element in your DataTemplate. You cannot use GetTemplateChild() method on the ListBoxItem in that situation, because that function only works for finding elements in the ControlTemplate of a Control (in fact, that function shouldn't be public, but protected, and used only by control's logic to get named PARTs).

Your only option then is to iterate through ListBoxItem visual children, until you find the desired named element. In WPF you would end doing something like that:

a) http://stackoverflow.com/questions/1182 ... ode-behind
b) http://msdn.microsoft.com/en-us/library ... .110).aspx

With NoesisGUI you can implement something similar to the FindVisualChild<T>() function, but remember to use the BaseComponent.As<T> to cast to the appropriate type.

Who is online

Users browsing this forum: Bing [Bot] and 0 guests