Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Getting a ListBoxItem reference from within the DataTemplate
6 replies. Latest Post by stevenjamesfrank65 on June 30, 2009.
(0)
stevenja...
Member
38 points
111 Posts
06-30-2009 1:46 PM |
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
128 points
19 Posts
06-30-2009 2:06 PM |
You can try to use VisualTreeHelper.GetParent(element) to get parent visual combo box item.
06-30-2009 2:21 PM |
Thanks for your response.
I've tried that already. The ItemTemplate for the ListBox, as specified in a DataTemplate Static Resource consists of:
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).
06-30-2009 2:36 PM |
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.
06-30-2009 2:43 PM |
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.
06-30-2009 3:16 PM |
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 : DependencyObject3 {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
06-30-2009 3:24 PM |
It must be due to the fact that my DataTemplate is a Resource of UserControl, not of the Grid...