我无法在我的中心部分获取文本框
代码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;
我试图获取x:名称,但无法找到
答案 0 :(得分:0)
因为HubSection包含的DataTemplate没有直接添加到可视化树中,以便获取它们:
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 = "";
}
但我建议你使用MVVM,这样可以让事情变得简单。