- Nir Hasson
- Posts: 71
- Joined:
- Contact:
Get List Box Item
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 -
But it is a private method..
Thanks
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 -
Code: Select all
ListBoxItem* ListBox::GetListBoxItemFromItem(Core::BaseComponent* item) const;
Thanks
-
sfernandez
Site Admin
- Posts: 3188
- Joined:
Re: Get List Box Item
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.
Code: Select all
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
- Posts: 71
- Joined:
- Contact:
Re: Get List Box Item
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.
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.
Re: Get List Box Item
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?
When the UI is rendered, the template appears to be properly applied to the listbox items. What could be causing GetTemplateChild to return null?
-
sfernandez
Site Admin
- Posts: 3188
- Joined:
Re: Get List Box Item
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.
Re: Get List Box Item
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:
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?
Code: Select all
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
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?
-
sfernandez
Site Admin
- Posts: 3188
- Joined:
Re: Get List Box Item
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.
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: No registered users and 6 guests