【发布时间】:2011-11-24 17:12:31
【问题描述】:
这是我在这个很棒的平台上的第一个问题,是的,我在这里搜索过如何做到这一点,这在我的情况下很少见,因为它应该可以工作,但我从 SelectedItem 得到的只是字符串(它的内容,是一个TextBlock)
这是 XAML 部分,它是一个包含 ListItems 堆栈的 ListBox:
<Button x:Name="propertyChooser" BorderBrush="{x:Null}" Height="90" Margin="22,379,21,0" OpacityMask="{x:Null}" Style="{StaticResource ButtonStyle1}" VerticalAlignment="Top" Content="" Foreground="Black" Click="propertyChooser_Click" >
<Button.Background>
<ImageBrush ImageSource="/AminoBlocks;component/Images/fillblock.png" />
</Button.Background>
</Button>
<Popup x:Name="PropertyPopUp" Margin="0,0,0,0" Height="400" Width="400" IsOpen="False" >
<!--ScrollViewer x:Name="PageScrollViewer1" Height="620" Width="400"-->
<Grid Height="400" Width="400" Background="White">
<ListBox x:Name="propertyPicker" Margin="0,0,0,0" Height="400" Width="400" SelectionChanged="propertyPicker_SelectionChanged">
<ListBoxItem>
<TextBlock x:Name="property1" Foreground="Black" FontSize="24" Height="45" Width="450" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Non-polar, aliphatic" TextAlignment="Center"/>
<!--Click="property1_Click"-->
</ListBoxItem>
<ListBoxItem>
<TextBlock x:Name="property2" Foreground="Black" FontSize="24" Height="45" Width="450" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Non-polar, aromatic" TextAlignment="Center"/>
<!--Click="property1_Click"-->
</ListBoxItem>
<ListBoxItem>
...
好的,列表有9个项目长,当你点击按钮时列表显示为一个弹出,然后你按下或选择属性和后面的代码,它假设将按钮内容更改为选择的属性在列表框中。到目前为止,我找到了如何做到这一点,但是我得到了空或 null 或内容的类型,即 selectedItem 的类型,列表框项的类型。我想要的只是我从 ListBox 中选择的 ListBoxItem 中的 TextBlock 的文本。
这个甚至背后的代码是:
private void propertyPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem selected = propertyPicker.SelectedItem as ListBoxItem; //Selected item.
string selectedText = selected.Content as String; //Content of the Item.
if (propertyPicker.SelectedItem != null)
propertyChooser.Content = selectedText; //(propertyPicker.SelectedItem as ListBoxItem).Content as string;
PropertyPopUp.IsOpen = false; //When the item get selected, close the pop up.
}
...
是的,这假设可以完成这项工作,但它并没有,正如我上面所说,它只是给我一个空的、null 或 selectedItem 的类型、listboxitem 的类型......我尝试了 long方式,正在实施的方式和短方式,在旁边注释的方式。
为什么不选择 SelectedItem,它的内容不会按原样传递,来自 TextBlock 的文本,这是 ListBox 作为 ListBoxItem 在其中包含的内容?
感谢任何回答我的菜鸟问题的人,如果我在发布这个或编码时犯了一个错误,请原谅我,我似乎不太明白,因为我在这里问,因为必须是逻辑或我的视线没有看到的其他类型的错误。
fr33
【问题讨论】:
标签: c# visual-studio silverlight xaml windows-phone-7