这是我要提取enjoymentID的图片
我想从数据绑定的选定项目中提取enjoymentID,但我尝试在web中可用的所有方式仍然无法提取它由于invalidCastException。有没有人可以帮我解决这个问题?紧急。谢谢= D
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SuperData data = (sender as ListBox).SelectedItem as SuperData;
ListBoxItem selected = this.listBox1.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
}
我试过这个,还有e.AddedItem [0],但仍然无法得到它。
<Grid>
<ListBox HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="listBox1" VerticalAlignment="Stretch" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<Image Source="{Binding image}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding title}" Foreground="#FFC8AB14" FontSize="28" />
<!--TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" /-->
<TextBlock Text="{Binding description}" TextWrapping="Wrap" FontSize="24" />
<TextBlock Text="Test" TextWrapping="Wrap" FontSize="24" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
这是我绑定数据的UI部分。从WCF返回的是 List 数据类型,我只是使用 listBox1.ItemsSource = e.Result; 绑定到列表框。
public int category { get; set; }
public int categoryField { get; set; }
public string description { get; set; }
public string descriptionField { get; set; }
public int enjoymentID { get; set; }
public int enjoymentIDField { get; set; }
public string image { get; set; }
public string imageField { get; set; }
public object PropertyChanged { get; set; } << i don't know what is this
public string title { get; set; }
public string titleField { get; set; }
这是e.AddedItem [0]内的内容也= D
答案 0 :(得分:1)
从您的屏幕截图中,数组的第一项的类型是RoyalEnjoyment.REServiceReference.REData,如果您将e.AddedItems [0]强制转换为此类型,那么您应该能够访问各种属性值。< / p>
例如,RoyalEnjoyment.REServiceReference.REData x =(RoyalEnjoyment.REServiceReference.REData)e.AddedItems [0];