0

I have the following listbox in my windows phone 8.1 silverlight application

        <ListBox  x:Name="ScenarioControl" FontFamily="Segoe WP">
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Name="myborder"  Margin="0,0,0,0" BorderThickness="0,0,0,3" BorderBrush="Black">
                        <Grid HorizontalAlignment="Stretch" Margin="0,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition Width="auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Tap="TextBlock_Tap" HorizontalAlignment="Left" VerticalAlignment="Center"  Text="{Binding Path=Title, Mode=OneTime}" FontFamily="Segoe WP Semibold" Grid.Column="0" FontSize="23"/>
                            <Image Tap="Image_Tap" Source="/Images/blogger_small.png" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="1" OpacityMask="Black"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

As you can see TextBlock bound dynamically. I need to change some properties for the last TextBlock element but I cannot find method to get exact element inside the code.

I did search and found that there are solutions with ItemContainerGenerator method but that does not work for me, the following line of code always returns null.

ListBoxItem item = this.ScenarioControl.ItemContainerGenerator.ContainerFromIndex(ScenarioControl.Items.Count - 1) as ListBoxItem;
Ashot Khachatryan
  • 1,543
  • 1
  • 12
  • 23
  • Has ScenarioControl been loaded before you try to get the containers? – A.R. Dec 30 '14 at 18:14
  • Yes, I put the code line mentioned above in ScenarioControl_Loaded event, but it does not help. – Ashot Khachatryan Dec 30 '14 at 18:18
  • Which properties of the TextBlock you would like to change? Have you tried [Style & Triggers](http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/) ? – stukselbax Dec 30 '14 at 18:22
  • For example I want to change weight of the last TextBlock. No I have not used triggers, I saw some examples but did not try yet. It seems interesting feature, but I am not sure if I can chose exact element with the triggers. – Ashot Khachatryan Dec 30 '14 at 18:29

2 Answers2

0

The most common cause of this problem is that the item in question isn't visible when you make the call to ItemContainerGenerator. Most listboxes, etc. virtualize their contents, meaning that they don't generate UI for elements that would not be visible to the user, so when you go to get the container, you just get null.

This Question + Answer(s) sums this up, and offers a solution to the problem.

Community
  • 1
  • 1
A.R.
  • 14,305
  • 17
  • 71
  • 121
0

Using a converter can help. It's not all declarative, but was what I thought initially.

Change style of last item in ListBox

Community
  • 1
  • 1
Stalzer
  • 104
  • 1
  • 3