Skip to main content

Microsoft Silverlight

Answered Question Getting a ListBoxItem reference from within the DataTemplateRSS Feed

(0)

stevenjamesfrank65
stevenja...

Member

Member

38 points

111 Posts

Getting a ListBoxItem reference from within the DataTemplate

I am working on a pretty simple ListBox that gets its items from a WCF service.  The items are retrieved and mapped into a couple of TextBlocks that are part of the ItemTemplate, for which I have a Static Resource DataTemplate set up.

Also part of the ItemTemplate is a button.  When the button is clicked, I need to get a reference to the ListBoxItem that contains the button (from within the Click handler of the button).

I've played around with the SelectedItem, but clicking the button does not force selection of the item so there is no guarantee that the button clicked will belong to the selected item.

steveli
steveli

Member

Member

128 points

19 Posts

Re: Getting a ListBoxItem reference from within the DataTemplate

 You can try to use VisualTreeHelper.GetParent(element) to get parent visual combo box item.

Steve Li,
Cellbi Software
www.cellbi.com

stevenjamesfrank65
stevenja...

Member

Member

38 points

111 Posts

Re: Getting a ListBoxItem reference from within the DataTemplate

Thanks for your response.

I've tried that already.  The ItemTemplate for the ListBox, as specified in a DataTemplate Static Resource consists of:

  • Border
    • Grid
      • TextBlock
      • Button

I can get the parent of the Button (the Grid), then get the parent of the Grid (the Border) but that is where the tree seems to end, the parent of the Border is null.  I'm not sure, but I don't think the parent reaches "outside" of the ItemTemplate (at least it seems like it doesn't).

steveli
steveli

Member

Member

128 points

19 Posts

Re: Getting a ListBoxItem reference from within the DataTemplate

Another idea:

Try MouseEnter event on ListBoxItem and store ListBoxItem inside a field, this way you know where mouse pointer was when the button was clicked.

However it looks like a workaround.

Steve Li,
Cellbi Software
www.cellbi.com

stevenjamesfrank65
stevenja...

Member

Member

38 points

111 Posts

Re: Getting a ListBoxItem reference from within the DataTemplate

I would agree, that is pretty much a work around.

There has to be a way to get this reference, hopefully someone out there knows how to do it.

steveli
steveli

Member

Member

128 points

19 Posts

Answered Question

Re: Getting a ListBoxItem reference from within the DataTemplate

I just tried using VisualTreeHelper.GetParent in simple app and it works fine, please see code below.

I wonder what is different from your app?

 

MainPage.xaml:

1        <Grid x:Name="LayoutRoot" Background="White">
2
3 <Grid.Resources>
4 <Style x:Name="MyItemTemplate" TargetType="ListBoxItem">
5 <Setter Property="Template">
6 <Setter.Value>
7 <ControlTemplate TargetType="ListBoxItem">
8 <Border>
9 <Button Content="{TemplateBinding Content}" Click="Button_Click"></Button>
10 </Border>
11 </ControlTemplate>
12 </Setter.Value>
13 </Setter>
14 </Style>
15 </Grid.Resources>
16
17 <StackPanel>
18 <ListBox>
19 <ListBoxItem Content="1" Style="{StaticResource MyItemTemplate}">
20 </ListBoxItem>
21 <ListBoxItem Content="2" Style="{StaticResource MyItemTemplate}">
22 </ListBoxItem>
23 </ListBox>
24
25 <TextBlock x:Name="text" Text="_"></TextBlock>
26 </StackPanel>
27
28 </Grid>

 

And  MainPage.xaml.cs:

 

1    T GetParent<T>(DependencyObject obj)
2 where T : DependencyObject
3 {
4 while (obj != null)
5 {
6 obj = VisualTreeHelper.GetParent(obj);
7 if (obj is T)
8 return (T)obj;
9 }
10 return null;
11 }
12
13 private void Button_Click(object sender, RoutedEventArgs e)
14 {
15 ListBoxItem item = GetParent<ListBoxItem>(sender as UIElement);
16 object content = item.Content;
17 text.Text = content.ToString();
18 }
19
  

Steve Li,

Cellbi Software

www.cellbi.com

Steve Li,
Cellbi Software
www.cellbi.com

stevenjamesfrank65
stevenja...

Member

Member

38 points

111 Posts

Re: Getting a ListBoxItem reference from within the DataTemplate

It must be due to the fact that my DataTemplate is a Resource of UserControl, not of the Grid...

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities