Ad

How To Access A Child Element Dynamically In WPF

- 1 answer

In WPF, I have a structure like this

<Button>
    <Grid>
        <!--definitions for 1 row and 2 columns-->
        <TextBlock x:Name="t1" Grid.Column="0"/>
        <TextBlock x:Name="t2" Grid.Column="1"/>
    </Grid>
</Button>

Supposed a Button b with this structure is generated dynamically. How to access t1 from Button b?

Edit for clarification: Since t1 resides within Button b, is it possible to change the content of t1 if one only have access to b? something along the line of b.childGridElement.childTextBlock_t1.Text = "newString"?

Ad

Answer

This should work for the use case you've provided:

((TextBlock)b.FindName("t1")).Text = "newString";

Ad
source: stackoverflow.com
Ad