I can't figure out how to do this correctly.
The "solution" here: "Get user input from textbox in WPF application" doesn't work, and it's about all I can find.
If I'm missing something simple, or if I'm simply going about this in entirely the wrong way please let me know.
Thanks in advance
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">
<TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Margin="114,48,125,158" TextChanged="textBox_TextChanged"/>
<Button x:Name="button" Content="Button" Margin="188,0,212,86" RenderTransformOrigin="0.5,0.5 Height="38" VerticalAlignment="Bottom" Click="button_Click"">
</Button> >
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void button_Click(object sender, RoutedEventArgs e)
{
/// trying to get textbox input via button press
/// this doesn't work:
string input = textBox_TextChanged.Text;
///neither does var input = textBox_TextChanged.Text
/// or anything else in the textBox_Textchanged position
/// I'm pulling my hair out...
}
}
}
textBoxis in fact a valid identifier and you can get theTextproperty from it. Note, however, that this whole bit of code falls into the "You're Doing It Wrong" category. You should be using WPF's data binding features to have theTextproperty automatically bound to the property of a view model, and when the button is clicked, you can get the current value straight from the view model. Then you don't have to name your controls at all or access their properties directly.