选择一项后,无法从列表框中获取字符串值。
在我的列表框中,我有图像(盲源)和TextBlock (盲源)。
我在.xaml页面上的代码:
<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参数)
{
MessageBox.Show(carListBox.SelectedItem.ToString());
string saveData = carListBox.SelectedItem.ToString();
}MessageBox不能显示字符串值,并且我不能在用户选择后获取值。
MessageBox显示1
发布于 2019-05-26 10:40:09
您必须以某种方式获取Car对象,然后才能访问它的属性。
如下所示:
private void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)
{
Car myCar=carListBox.SelectedItem as Car;
if(myCar != null)
MessageBox.Show(myCar.ShowDetail); //or any property.
}https://stackoverflow.com/questions/56310010
复制相似问题