Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:20:10+00:00 2026-06-18T11:20:10+00:00

I have a TextBox in a WPF Window that I populate from the code

  • 0

I have a TextBox in a WPF Window that I populate from the code behind (with a file name after the user has selected one from an OpenFileDialog.

Although my TextBox.Text is bound to a String property in my ViewModel class, the property is not populated when the text is set into TextBox.Text.

If I type into the TextBox, then the property is populated, so there must be some event firing or something that happens during user input and not when I set the value via the code.

What is the the step I’m missing to properly bind?

Also, is the set method on the property meant to be called when the property is changed, or the UI? Or both? If changing the UI calls the set method, which fires the PropertyChanged event to update the UI, what stops this looping forever?

(I know I could set the property directly, but I feel I’m lacking understanding on binding and I’m hoping this will help fill some gaps.)

My sample code:

<Window x:Class="ConfigurationViewer.ViewerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ConfigurationViewer"
    Title="Configuration Viewer" Height="512" Width="714" >
    <Window.DataContext>
        <local:TaskViewModel x:Name="_model"/>
    </Window.DataContext>
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <TextBlock Text="Configuration file" Margin="2" VerticalAlignment="Center" />
            <TextBox Height="29" Margin="2" Name="textFilePath" Width="277" Text="{Binding Path=ConfigurationPath}" />
            <Button Content="Browse ..." Margin="2" Name="buttonBrowseFile" Width="98" Click="buttonBrowseFile_Click" />
            <Button Content="Open" Margin="2" Name="buttonOpenFile" Width="98" Click="buttonOpenFile_Click"  />
        </StackPanel>
    </DockPanel>
</Window>

    public partial class ViewerWindow : Window
    {
        public ViewerWindow()
        {
            InitializeComponent();
        }

        private void buttonBrowseFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Multiselect = false;
            openDialog.InitialDirectory = textFilePath.Text;

            Nullable<Boolean> ok = openDialog.ShowDialog();
            if (ok == true)
            {
                textFilePath.Text = openDialog.FileName;
            }
        }

        private void buttonOpenFile_Click(object sender, RoutedEventArgs e)
        {
        }
    }

    public class TaskViewModel : INotifyPropertyChanged
    {
        private String _configurationPath = String.Empty;
        public String ConfigurationPath
        {
            get { return _configurationPath; }
            set
            {
                _configurationPath = value;
                OnPropertyChanged("ConfigurationPath");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String prop)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(prop);
                handler(this, e);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:20:11+00:00Added an answer on June 18, 2026 at 11:20 am

    The reason that the ViewModel property isn’t updated when you set the TextBox.Text property is that the UpdateSourceTrigger property of the Binding is set to LostFocus. This is the default value for bindings on the TextBox.Text property.

    If you change it to PropertyChanged, the ViewModel property will be updated as you expected:

    <TextBox Text="{Binding ConfigurationPath, UpdateSourceTrigger=PropertyChanged}"/>
    

    And of course the WPF binding system takes care to avoid infinite update loops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF Window that contains a TextBox. I have implemented a Command
I have an WPF usercontrol that has a TextBox. I set the text Underline
I have a UserControl in WPF that contain a textBox. My user control is
I'm using MVVM in my WPF application. I have a Window that has a
I have a TextBox in a WPF window bound to a dependency property of
I have a WPF textBox that is declared as ReadOnly <TextBox IsReadOnly=True IsTabStop=False Width=200
I have a WPF TextBox in which I want to allow autocompleting user names
Desktop window application. I have textbox in which user give values in numbers. what
I have a WPF Window, and in that window I have a grid. I
I have a WPF window with a button that spawns a BackgroundWorker thread to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.