C# 从所选ListBoxItem模板获取Textblock值

C# 从所选ListBoxItem模板获取Textblock值,c#,uwp,win-universal-app,windows-10-universal,uwp-xaml,C#,Uwp,Win Universal App,Windows 10 Universal,Uwp Xaml,有人知道如何从运行时生成的listboxitem中提取textblock文本吗 我定制了一个ListBoxItem,其中包含一个SymbolIcon和一个textBlock。 我只需要textBlock值 private void AlbumSongList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = AlbumSongList.SelectedItem as

有人知道如何从运行时生成的listboxitem中提取textblock文本吗

我定制了一个ListBoxItem,其中包含一个SymbolIcon和一个textBlock。 我只需要textBlock值

private void AlbumSongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selected = AlbumSongList.SelectedItem as ListBoxItem;
        DataTemplate template = selected.ContentTemplate;


        Debug.WriteLine("You have selected the song: " + selected.ToString());
    }
下面是包含textblock的自定义控件

<Style x:Key="CustomListBoxItem1" TargetType="ListBoxItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="Padding" Value="12,11,12,13"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid.Resources>
                                <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
                                    <Setter Property="FontFamily" Value="Segoe UI"/>
                                    <Setter Property="FontWeight" Value="Light"/>
                                    <Setter Property="FontSize" Value="15"/>
                                    <Setter Property="TextWrapping" Value="NoWrap"/>
                                    <Setter Property="LineStackingStrategy" Value="MaxHeight"/>
                                    <Setter Property="TextLineBounds" Value="Full"/>
                                    <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings"/>
                                    <Setter Property="Height" Value="30" />
                                </Style>
                                <Style x:Key="BodyContentPresenterStyle" BasedOn="{StaticResource BaseContentPresenterStyle}" TargetType="ContentPresenter">
                                    <Setter Property="FontWeight" Value="Normal"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </Grid.Resources>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Rectangle Grid.Column="2" x:Name="PressedBackground" Fill="Transparent" Control.IsTemplateFocusTarget="True"/>
                            <SymbolIcon Symbol="MusicInfo" />

                            <TextBlock Grid.Column="1" Text="{Binding SongNumber}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0" Text="{Binding Song}" FontSize="15"/> <!--TextBlock Needed-->

                            <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Style="{StaticResource BodyContentPresenterStyle}" TextWrapping="NoWrap" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedUnfocused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentMediumBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedBackground" Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>


                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

同样,如果您看到文本绑定源为Sing的“Textblock”,这就是我需要的文本值。

有一个默认的选择行为,这取决于项源是什么(所使用的类型)。因此,是您的
模型
类的类型,因此您可以从所选模型中获取的文本

例如,您的模型类名为
model
,然后您可以使用以下代码获取文本

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var selected = AlbumSongList.SelectedItem as Model;

    Debug.WriteLine("You have selected the song: Song Number is {0}, and Song is {1}",
        selected.SongNumber, selected.Song);
}

你能提供专辑歌曲列表的详细信息吗?我已经更新了,如果你能帮忙的话,非常感谢,对我来说非常有用。你是个明星