【问题标题】:Why the SelectedItem is not extracted as usual?为什么 SelectedItem 没有像往常一样被提取?
【发布时间】: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


    【解决方案1】:

    ListBox 的内容是文本块。尝试将 SelectedItem 转换为 TextBlock,然后从中获取文本。

    【讨论】:

    • 哇,谢谢先生,你真是个天才:)
    • @fr33 以前没有天才。如果答案有效,请接受
    【解决方案2】:

    您选择的 ListBoxItem 的内容是 TextBlock,而不是字符串。线

    string selectedText = selected.Content as String; //Content of the Item.
    

    需要:

    string selectedText = ((TextBlock)selected.Content).Text; //Content of the Item.
    

    【讨论】:

    • 嘿,谢谢伙计,我已经按照@Mark Hall 告诉我的提示做到了:)
    猜你喜欢
    • 2016-09-04
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2012-11-01
    • 1970-01-01
    • 2018-10-11
    • 2017-01-04
    相关资源
    最近更新 更多