Find parent of paragraph UWP

1

I would like to find the parent of the paragraph with the name: "ParagraphOne" (hence the RichTextBlock which has the name: "RichTextOne") but the code generates an exception.

Exception

Xaml:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel>
            <RichTextBlock x:Name="RichTextOne" Margin="30">
                <Paragraph x:Name="ParagraphOne">
                    <Run Text="This is a Text"/>
                </Paragraph>
            </RichTextBlock>
            <Button x:Name="btnParent" Click="btnParent_Click" Margin="30" Width="100" Height="32" Content="Find Parent"/>
        </StackPanel>
    </Grid>
</Grid>

Code:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void btnParent_Click(object sender, RoutedEventArgs e)
    {
        var parent = VisualTreeHelper.GetParent(ParagraphOne) as UIElement;
        string pName = (parent as RichTextBlock).Name;
        var messageDialog = new MessageDialog("The name of parent is: " + pName);
        await messageDialog.ShowAsync();
    }
}

a way? Thanks in advance!

The error that is generated is: Irreparable error (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

c#
uwp
asked on Stack Overflow Dec 19, 2017 by Tibrus • edited Dec 19, 2017 by Tibrus

1 Answer

2

This should work:

RichTextBlock parent = ParagraphOne.ElementStart.Parent as RichTextBlock;
answered on Stack Overflow Dec 20, 2017 by A. Milto

User contributions licensed under CC BY-SA 3.0