列表框中项目模板中的 Selecteditem 事件

发布于 2025-01-05 16:57:56 字数 1284 浏览 3 评论 0原文

我需要在列表框项模板内更改选择事件。我的列表框由三个文本块和一个图像组成。我只想获取第三个文本块文本,当我选择第三个文本块时,文本块中的文本将显示为弹出窗口。

我使用可视化树来搜索文本块,但它采用第一个文本块的值而不是第三个文本块。我可以做什么来获取第二个和第三个文本块的值。仅当我单击列表框中的文本块而不是整个列表框项目时,我才需要触发弹出窗口。

<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>  

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>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美不过初阳 2025-01-12 16:57:56

使用 TextBlock_MouseLeftButtonUp 或 TextBlock_Tap(这是单击文本块时上升)

在此,

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

上面的字符串显示在您想要的位置。

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.

蓝海似她心 2025-01-12 16:57:56

您可能应该将有问题的 TextBlock 包装在 Button 中或向其添加 Hyperlink。两者都支持命令并具有 Click 事件。 (要使 Button 不可见,您可以将 Template 重写为仅简单的 ContentPresenter

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文