Accessing a text box in WPF

DeepSpace

New member
Joined
Feb 5, 2025
Messages
1
Programming Experience
Beginner
Hey All,

Being new to the C#, Visual Studio/WPF environment, I'm struggling to find the right resources to get answers to some basic questions. So I appreciate any help with answers or references to other resources.
I'm creating a test application using WPF and I have a textbox that I would like to use for displaying data.
The application will query a piece of test equipment for voltage, current an power, then I would like to display that data in the textbox.
VS has created the basic xaml for the window form and basic C# code, but how do I actually post data in that window?
How do I establish a reference to that textbox (named PowerValues) so that I can write the power data to that textbox?

Sorry for this basic question, but hey, ya gotta start somewhere.
Thank you in advance for any help.

Here is the code created by VS:
C#:
using System.Text;
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 CT_3_USB_Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
 
Last edited by a moderator:
Yes. Gotta start somewhere:
 
Considering you're using WPF, you should also think about using MVVM. This is a programming paradigm, which encapsulates the view from the codebehind. Essentially the view (what you see) doesn't know where it's data comes from.
This is what WPF's data bindings are for.
Let's say you have two text boxes, where the code from one should fill the other.
There are your two TextBoxes:
Data textbox:
<TextBox Text="{Binding MyFirstTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{DynamicResource MyVM}" />
<TextBox Text="{Binding MySecondTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{DynamicResource MyVM}" />

In your window resources, you have defined an object which is your view model:
defined an object which is your view model::
<Window.Resources >
    <vm:MyViewModel x:Key="MyVM" />
</Window.Resources>

Your view model could then look something like this:
View Model::
public class MyViewModel: INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propName) {
        PropertyChanged?.Invoke(this, new OnPropertyChangedEventArgs(propName));
    }

    private string textBox1Text;

    public string MyFirstTextBox {
        get => textBox1Text;
        set {
            textBox1Text = value;
            OnPropertyChanged(nameof(MyFirstTextBox));
            MySecondTextBox = value; // Set value of second text box
        }
    }

    private string textBox2Text;

    public string MySecondTextBox {
        get => textBox2Text;
        set {
            textBox2Text = value;
            OnPropertyChanged(nameof(MySecondTextBox));
        }
    }

}

Now you'd have a very basic MVVM implementation, which should help you further.

If you have no interest in using MVVM, then you can continue using events.

In your codebehind:
C#:
private void InputText_TextChanged(object sender, EventArgs e) => OutputText.Text = (sender as TextBox).Text;

private void OutputText_TextChanged(object sender, EventArgs e) => Debug.WriteLine("Whoo! My text changed to {0}", (sender as TextBox).Text);

EDIT: Added last stretch of code to code tags. Also edit: Sorry if I made a mistake in the code, this was off the top of my head during lunch break.
 
Back
Top Bottom