sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

Access UI Element programmatically UWP

I have some controls in my xaml file of my c++ universal application. I want to access the elements and set some property values.

<PivotItem Header="Home">
  <Grid>
   <Grid.ColumnDefinitions>
   <ColumnDefinition Width="4*"></ColumnDefinition>
   <ColumnDefinition Width="3*"></ColumnDefinition>
   </Grid.ColumnDefinitions>
   <StackPanel Padding="8,8,8,8">
    <TextBlock Margin="8" Text="A"/>
    <Border Margin="8,0,0,0" HorizontalAlignment="Left" BorderBrush="Gray" BorderThickness="2" Grid.Row="0">
     <TextBox x:Name="controlA" x:Uid="controlA" HorizontalAlignment="Left"  Width="160" Text="100 kHz" IsEnabled="False" IsReadOnly="True"/>
    </Border>
   </StackPanel>
  </Grid>
</PivotItem>

Is it possible to access my controls programmatically.

I did it finally as below following the accepted answer:

//Code For a text box
for (auto&& child : tstStack->Children)
{                                   
   TextBlock^ temp = dynamic_cast<TextBlock^>(static_cast<Object^>(child));
}

Upvotes: 0

Views: 622

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

In addition to the x:Name name that is promoted to a field in your class, you can walk the tree using element-specific properties. For example, StackPanel->Children returns a collection of child elements that you can add, remove, enumerate, etc.

Upvotes: 1

Related Questions