首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在用户选择时从列表框中获取字符串值?

如何在用户选择时从列表框中获取字符串值?
EN

Stack Overflow用户
提问于 2019-05-26 09:34:26
回答 1查看 63关注 0票数 0

选择一项后,无法从列表框中获取字符串值。

在我的列表框中,我有图像(盲源)和TextBlock (盲源)。

我在.xaml页面上的代码:

代码语言:javascript
复制
        <ListBox Name="carListBox" Height="431" Canvas.Left="28" Canvas.Top="65" Width="446" SelectionChanged="ListBoxOnSelection">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Name="brandImage" Source="{Binding Image}" Width="100" Height="150"></Image>
                        <Image Name="carImage" Source="{Binding Image}" Width="150" Height="150"></Image>
                        <TextBlock Name="textDisplay" Text="{Binding ShowDetail}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我在.xaml.cs页面(C#)的代码

private void ListBoxOnSelection(对象发送者,SelectionChangedEventArgs参数)

代码语言:javascript
复制
    {
       MessageBox.Show(carListBox.SelectedItem.ToString());
       string saveData = carListBox.SelectedItem.ToString();

    }

MessageBox不能显示字符串值,并且我不能在用户选择后获取值。

MessageBox显示1

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-26 10:40:09

您必须以某种方式获取Car对象,然后才能访问它的属性。

如下所示:

代码语言:javascript
复制
private void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)
{
 Car myCar=carListBox.SelectedItem as Car;
 if(myCar != null)
   MessageBox.Show(myCar.ShowDetail); //or any property.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56310010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档