views:

496

answers:

3

I've got a ListBox in Silverlight2 that is using a Grid as its ItemsPanelTemplate. In code, how do I get a reference to that Grid?

<ItemsPanelTemplate x:Key="GridItemsPanel">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

<ListBox ItemsPanel="{StaticResource GridItemsPanel}" />

Background:

What I am trying to do with this is add children to the Grid that are not ListBoxItems. I am trying to get something similar to the Outlook Calendar day view, where items can be aligned on a grid with a resolution of 30 minutes. I have the grid and the ListBoxItem positioning working correctly, however I'd like to be able to render background colours, gridlines, hour labels etc to allow the user to clearly see the time and duration of the ListBoxItems.

A: 

You need to give the Grid a name with the x:Name attribute:

http://msdn.microsoft.com/en-us/library/cc189028(VS.95).aspx

Jonathan Parker
Do you mean give the Grid in the ItemsPanelTemplate an x:Name? If I do that I cant find it using FindName("gridname") within the subclassed listbox.
geofftnz
I think you need to call FindName on the particular listbox item.
Jonathan Parker
The grid is the itemspanel, so there should only be one for the entire listbox.
geofftnz
FindName won't discover it because its a template item which means that its name does not exist in the same namescope for the main visual tree. Even though in this case there is only one copy of the template item, templates are designed to be cloned multiple times and it wouldn't do attempt to add to the visual tree items that have duplicate names. For example you might have multiple ListBox controls all referencing the same template.
AnthonyWJones
A: 

If your goal is to create listbox with custom rendering here is an example:

<ListBox x:Name="lstMails">
    <ListBox.ItemTemplate>
     <DataTemplate>
      <Grid Background="Gray">
       <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
       </Grid.ColumnDefinitions>

       <TextBlock Grid.Column="0" Text="{Binding Path=Subject}" />
       <TextBlock Grid.Column="1" Text="{Binding Path=Sender}" />
       <TextBlock Grid.Column="2" Text="{Binding Path=SentTime}" />
      </Grid>
     </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And set ItemsSource property from code behind or by specifying binding:

lstMails.ItemsSource = /*collection of Mail objects*/;

Hope, this was helpful.

Alexander K.
Thanks, but what I'm trying to do is style the itemspanel independent of the listitems. If you think of the calendar in outlook, you've got background shading and gridlines behind the calendar entries. That's what I'm after.
geofftnz
A: 

If you really just want to get a reference, regardless of namescope, you can use VisualTreeHelper to walk through it. I did this a while back using a custom wrapper

http://blogs.vertigo.com/personal/jimbg/Blog/archive/2008/10/24/walk-the-visual-tree.aspx

Jim B-G