【发布时间】:2017-07-21 08:19:10
【问题描述】:
我尝试在我的 XAML 数据模板中定义的按钮单击事件上从 TextBox 中获取值,如下所示:
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:Ausstattung">
<Grid Height="40" Width="Auto" Background="LightSlateGray" >
<TextBlock Grid.Column="0" Foreground="White" FontSize="14" Text="{x:Bind Beschreibung}" HorizontalAlignment="Center" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"/>
<TextBlock Grid.Column="1" Foreground="White" FontSize="14" Text="{x:Bind Ausgabedatum}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Column="2" Foreground="White" FontSize="14" Text="{x:Bind Rückgabedatum}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBox Grid.Column="3" Foreground="White" FontSize="14" x:Name="txtAnzahl" PlaceholderText="{x:Bind Anzahl}" TextChanged="TextBox_OnTextChanged" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<StackPanel Orientation="Horizontal" Grid.Column="4" Margin="-25">
//here I want to get the value from the TextBox named "txtAnzahl"
<Button Height="30" Width="30" Margin="0,10,10,10" Padding="0" Click="ButtonBase_OnClick">
<TextBlock FontFamily="Segoe MDL2 Assets" Foreground="LimeGreen" Text="" FontSize="20"/>
</Button>
<Button Height="30" Width="30" Margin="0,10,10,10" Padding="0">
<TextBlock FontFamily="Segoe MDL2 Assets" Foreground="DarkRed" Text="" FontSize="16"/>
</Button>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
所以我尝试从按钮 OnClick 事件的文本框“txtanzahl”中获取值。
我尝试使用 VisualTreeHelper 来完成它,但我只找到了 GetChild 或 GetParent 的示例,但在这种情况下,它既不是孩子也不是父母。
我也无法像这样获得具有给定名称“txtAnzahl”的控件:
var anzahl = txtAnzahl.Text;
它说他不知道这个元素。
【问题讨论】:
-
这是 UWP 还是 WPF?
-
它是 UWP........