当我点击更新按钮,然后我想获得数据模板内的文本框的值。
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Username:" VerticalAlignment="Top" Height="30" Width="176" FontSize="20"/>
<TextBox Text="{Binding Username}" x:Name="tb41" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,4,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="174" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Password:" VerticalAlignment="Top" Height="28" Width="176" FontSize="20"/>
<TextBox Text="{Binding Password}" x:Name="tb42" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,0,0,1.333" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="10,10.667,0,0" Grid.Row="2" TextWrapping="Wrap" Text="Confirm Password:" VerticalAlignment="Top" Height="32" Width="176" FontSize="20"/>
<TextBox Text="{Binding ConfirmPassword}" x:Name="tb43" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,11.5,0,0" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="10,10.167,0,0" Grid.Row="3" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Top" Height="31" Width="176" FontSize="20"/>
<TextBox Text="{Binding Name}" x:Name="tb44" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,10.167,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="10,10.333,0,0" Grid.Row="4" TextWrapping="Wrap" Text="Age:" VerticalAlignment="Top" Height="29" Width="176" FontSize="20"/>
<TextBox Text="{Binding Age}" x:Name="tb45" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,8.333,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="0,10.167,0,0" Grid.Row="5" TextWrapping="Wrap" Text="Mobile no:" VerticalAlignment="Top" Height="29" Width="186" FontSize="20"/>
<TextBox Text="{Binding Mobile}" x:Name="tb46" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,8.167,0,0" Grid.Row="5" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="0,10,0,0" Grid.Row="6" TextWrapping="Wrap" Text="Email id" VerticalAlignment="Top" Height="32" Width="186" FontSize="20"/>
<TextBox Text="{Binding Email}" x:Name="tb47" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,8.667,0,0" Grid.Row="6" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="0,10.333,0,0" Grid.Row="7" TextWrapping="Wrap" Text="Organisation:" VerticalAlignment="Top" Height="30" Width="186" FontSize="20"/>
<TextBox Text="{Binding Organisation}" x:Name="tb48" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,8,0,0" Grid.Row="7" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" Margin="0,10.333,0,0" Grid.Row="7" TextWrapping="Wrap" Text="Id:" VerticalAlignment="Top" Height="30" Width="186" FontSize="20"/>
<TextBox Text="{Binding Id}" x:Name="tb49" Grid.Column="1" HorizontalAlignment="Left" Margin="10.167,8,0,0" Grid.Row="7" TextWrapping="Wrap" VerticalAlignment="Top" Width="174"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="Reset" HorizontalAlignment="Left" Margin="56,54,0,0" Grid.Row="7" Grid.RowSpan="2" VerticalAlignment="Top"/>
<Button Content="Update" Grid.Column="1" HorizontalAlignment="Left" Margin="46.167,54,0,0" Grid.Row="7" Grid.RowSpan="2" VerticalAlignment="Top"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:0)
有不同的方法可以实现这一点,这里有一个:
<Button Content="Update" Tag={Binding ElementName=tb49} Click="Button_Click" />
并在后面的代码中:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
TextBox textBox = (TextBox)btn.Tag;
string text = textbox.Text
}
您还可以使用按钮的DataContext直接访问绑定到的对象的属性。