noesis_user
Topic Author
Posts: 14
Joined: 08 May 2014, 00:04

Access component in style of ListboxItem

23 May 2014, 18:23

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 ?
 
User avatar
sfernandez
Site Admin
Posts: 1970
Joined: 22 Dec 2011, 19:20

Re: Access component in style of ListboxItem

23 May 2014, 19:32

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.

Who is online

Users browsing this forum: No registered users and 3 guests