简体   繁体   中英

Get textbox inside hub section

I cant get the textbox inside my hub section

code xaml :

      <Hub Header="Chutometro" x:Name="Hub" Margin="0,27,0,-1">
        <HubSection Header="Números" x:Name="HbNumemro">
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                     </Grid.RowDefinitions>
                     <TextBlock Text="De" Width="30" Height="30" FontSize="25" Margin="0,0,310,600"></TextBlock>
                     <TextBox x:Name="TxtN1" InputScope="Number" Height="40" MaxLength="15" Margin="0,30,0,0" ></TextBox>
                     <TextBlock Text="Á" Width="30" Height="30" FontSize="25" Margin="0,0,310,430"></TextBlock>
                     <TextBox x:Name="TxtN2" InputScope="Number" Height="40" MaxLength="15" Margin="0,110,0,0" ></TextBox>
                     <Button x:Name="BtN" Tapped="BtN_Tapped" Width="80" Height="80" Content="Okay" Margin="0,120,0,337"></Button>
                     <TextBlock x:Name="TxtBlockResult" FontSize="40" Height="80" Margin="0,280,0,269"></TextBlock>
            </Grid>
        </DataTemplate>
    </HubSection>
    <HubSection Header="Letras">

   </HubSection>
</Hub>

C# :

string txtn1 = TxtN1.Text;
string txtn2 = TxtN2.Text;

I'm trying to get the x: name, but cant find

Because The HubSection contains a DataTemplate is not added directly to the visual tree, in order to get them:

  private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        var textboxes = (sender as Grid).Children.OfType<TextBox>();
        var TxtN1 = textboxes.First(p => p.Name == "TxtN1");
        var TxtN2 = textboxes.First(p => p.Name == "TxtN2");

        string k = "";
    }

But I recommend you to use MVVM that will make things easy at long time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM