SENTHIL KUMAR
SENTHIL KUMAR

Reputation: 647

Selecteditem event in an itemtemplate inside a listbox

I need to have selection changed event inside a listbox itemtemplate. My listbox consists of three textblocks and an image. I want to get the third text block text only and when I select the third textblock the text in the text block will appear as a popup.

I used the visual tree to search for a textblock but it take the value of first text block instead of third textblock. What can i do to get the value of 2nd and 3rd textblocks. And I need to trigger a popup only when I click the textblock in the listbox not the whole listbox item.

<ListBox Name="listBox1" Width="Auto" SelectionChanged="Listbox1_SelectionChanged"> 
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Height="165" HorizontalAlignment="Left" Margin="10,40,-400,0"  VerticalAlignment="Top" Width="175" Source="{Binding thumb}"/>
                <TextBlock Name="pagetext"  TextWrapping="Wrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-135,-200,0" Text="{Binding page}" Foreground="#FF170101" />
                <TextBlock Name="titletext" Width="1000" TextWrapping="NoWrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-167,-200,0" Text="{Binding title}" Foreground="#FF170101" />
                <TextBlock Name="text" Width="1000" TextWrapping="NoWrap"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="195,-167,-200,0" Text="{Binding title}" Foreground="#FF170101" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>   
</ListBox>  

Upvotes: 1

Views: 1079

Answers (2)

brunnerh
brunnerh

Reputation: 184526

You should probably wrap the TextBlock in question in a Button or add an Hyperlink to it. Both support commands and have a Click event. (To make the Button invisble you can override the Template to be a simple ContentPresenter only)

<Button>
    <Button.Template>
        <ControlTemplate TargetType="Button"><ContentPresenter/></ControlTemplate>
    </Button.Template>
    <TextBlock .../>
</Button>
<TextBlock>
    <!-- In SL you probably need a Run inside the Hyperlink and it may not be bindable -->
    <Hyperlink Text="{Binding title}" .../>
</TextBlock>

Upvotes: 1

Pavan Kumar
Pavan Kumar

Reputation: 206

use TextBlock_MouseLeftButtonUp or TextBlock_Tap(this is rise when click on textblock)

In this,

    private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        TextBlock t = (TextBlock)sender;
        string s= t.Text;
    }

the above string s disply where you want.

Upvotes: 1

Related Questions