Page 1 of 1

Access component in style of ListboxItem

Posted: 23 May 2014, 18:23
by noesis_user
Hello,
I am applying a style to listbox item and then adding it to listbox. style is:(shorter version for
convenience)
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
	<ControlTemplate TargetType="{x:Type ListBoxItem}">
		<Grid x:Name="GridListItem" Height="Auto" Focusable="True">
			<Button x:Name="ButtonDelete" Content="Button" 
                HorizontalAlignment="Right" Height="Auto" Margin="0"
                Style="{DynamicResource DeleteButtonStyle}" 
                VerticalAlignment="Stretch" Width="25" Visibility="Visible" 
                IsHitTestVisible="True" Focusable="True"/>
			<Path Data="M40.5,25.5 L188.50084,25.5" Fill="#00BCCFEA" Height="Auto" 
                Stretch="Fill" Stroke="#FF556285" Width="Auto" Margin="0" 
                VerticalAlignment="Bottom"/>
		</Grid>
	</ControlTemplate>
</Style>
Applying style to listbox:
Ptr<IResourceKey> key = ResourceKeyString::Create("ListBoxItemStyle");
temp_ = p1_root->FindResource<Noesis::Gui::Style>(key.GetPtr());
list_box_->SetItemContainerStyle(temp_);
Getting button:
int index = list_box_->GetSelectedIndex();
Noesis::Gui::FrameworkElement* l = NsStaticCast<Noesis::Gui::FrameworkElement*>
    (list_box_->GetItems()->Get(index));
Noesis::Gui::Button* b1 = NsStaticCast<Noesis::Gui::Button*>(l->GetTemplateChild("ButtonDelete")); 
But it always returns NULL. Any pointers ?

Re: Access component in style of ListboxItem

Posted: 23 May 2014, 19:32
by sfernandez
How do you provide the items to the ListBox?
Are you using the ItemsSource property?
Or are you adding the items yourself via GetItems().Add()?
Which type of item are you adding?

Remember that the GetItem() return the item instance, that is not always the ListBoxItem container. And you have to invoke the GetTemplateChild() function over the ListBoxItem to find that button you are looking for.