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.
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))
This should work:
RichTextBlock parent = ParagraphOne.ElementStart.Parent as RichTextBlock;
User contributions licensed under CC BY-SA 3.0