tags:

views:

1545

answers:

3

Hello

I have some problem find right TextBlock control inside a StackPanel. My markup:

                        <ListBox Name="lstTimeline" ItemContainerStyle="{StaticResource TwItemStyle}" MouseDoubleClick="lstTimeline_MouseDoubleClick">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <DockPanel MaxWidth="{Binding ElementName=lstTimeline, Path=ActualWidth}">
                                    <Border Margin="10" DockPanel.Dock="Left"  BorderBrush="White" BorderThickness="1" Height="48" Width="48" HorizontalAlignment="Center">
                                        <Image Source="{Binding ThumbNail, IsAsync=True}" Height="48" Width="48" />
                                    </Border>
                                    <StackPanel Name="stkPanel" Margin="10" DockPanel.Dock="Right">
                                        <TextBlock Text="{Binding UserName}" FontWeight="Bold" FontSize="18" />
                                        <TextBlock Text="{Binding Text}" Margin="0,4,0,0" FontSize="14" Foreground="#c6de96" TextWrapping="WrapWithOverflow" />
                                        <TextBlock Text="{Binding ApproximateTime}" FontSize="14" FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB" />
                                        <TextBlock Text="{Binding ScreenName}" Name="lblScreenName"  FontSize="14" FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB" Loaded="lblScreenName_Loaded" />
                                    </StackPanel>
                                </DockPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

My dobule click code:

        private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);

        StackPanel item = lbi.FindName("stkPanel") as StackPanel;
        if (item != null)
            MessageBox.Show("StackPanel null");
        TextBlock textBox = item.FindName("lblScreenName") as TextBlock;
        if (textBox != null)
            MessageBox.Show("TextBlock null");

        MessageBox.Show(textBox.Text);
    }

But StackPanel are null? How do find the right textblock in selecteditem

Thanks for your help.

+3  A: 

There's a specific function to use when you're looking for something whose name is defined in a template. Try it like this:

private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);

    StackPanel item = Template.FindName("stkPanel",lbi) as StackPanel;
    if (item != null)
        MessageBox.Show("StackPanel null");
    TextBlock textBox = Template.FindName("lblScreenName",item) as TextBlock;
    if (textBox != null)
        MessageBox.Show("TextBlock null");

    MessageBox.Show(textBox.Text);
}
David Hay
StackPanel returns null (Value = null)
Frozzare
A: 

How are you binding the ItemsSource of your ListBox? I don't see it being set in XAML. Are there actually items in your ListBox? If not then you will always get a null with the code you have

Foovanadil
A: 

Linq to xml with a get and set model.

var item = ...

            lstTimeline.SelectedIndex = -1;
            lstTimeline.ItemsSource = item;
Frozzare
I solved it with this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7d2b4134-e460-4daa-86b7-24a629d77718
Frozzare