如何在ListBox的SelectionChangeEvent中从textBlock获取文本? [英] How to get text from textBlock during SelectionChangeEvent from a ListBox?

查看:56
本文介绍了如何在ListBox的SelectionChangeEvent中从textBlock获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从列表框中选择项目时,我尝试从TextBlock中获取文本.

I am try to get a text from my TextBlock during item select from my Listbox.

这是我的xaml文件中用于列表框的代码.

Here is my code for listbox in my xaml file..

<ListBox x:Name="listBox" Height="535" Margin="7,0,12,0" 
      SelectionChanged="selectedItem" 
      ItemsSource="{Binding}">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit:WrapPanel FlowDirection="LeftToRight" 
                ItemWidth="215" ItemHeight="215" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate x:Uid="info">
        <DataTemplate>
            <StackPanel>
                <Border>
                    <Image HorizontalAlignment="Left" Margin="6,6,0,0" 
                        Name="image1" Stretch="Fill" 
                        VerticalAlignment="Top" 
                        Source="{Binding imageSource}" />
                </Border>
                <TextBlock x:Name="path" Foreground="Transparent" 
                    Text="{Binding imagePath}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

对于我的C#代码,我使用的是selectionChangedEvent方法,该方法将处理点击事件,但是我不知道如何获取TextBlock元素的内容.

For my c# code i am using a selectionChangedEvent method that will handle tap event but i can not figure out how to get content of my TextBlock element.

private void selectedItem(object sender, SelectionChangedEventArgs e)
{
    //I need to take the content of my textblock w/c carry the path for
    //my image to be use to share using ShareMediaTask.    
    //path = the content of my textblock

    var task = new ShareMediaTask();
    task.FilePath = path;
    task.Show();
}

我将非常感谢您的帮助.

I would really appreciate any help.

推荐答案

如果imagePath是类的属性,请尝试

if imagePath is a Property of a class, try this

var item = listBox.SelectedItem as className; // like ShareMediaTask

var task = new ShareMediaTask();
task.FilePath = item.imagePath;
task.Show();

这篇关于如何在ListBox的SelectionChangeEvent中从textBlock获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆