Answered how to update textbox

  • Tuesday, February 28, 2012 5:58 PM
     
      Has Code

    I'm new to WPF and am trying to update a textbox after a property has changed.

    The textbox is bound in the MainWindow after the InitializeComponents is ran here

                Binding URLBinding = new Binding("FilePath");
                URLBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                txtURL.SetBinding(ContentProperty, URLBinding);

    and a button activates a FileDialogue to allow the textbox to gain a value here

        private void btnFilePath_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.DefaultExt = ".xml";
                dlg.Filter = "Comprehensive Report (.xml)|*.xml";
                if (dlg.ShowDialog() == true)
                {
                    tempJobItem.FilePath = dlg.FileName;
                }
            }

    but this doesn't cause the txtURL to list the new value for the FilePath property.  What am I doing wrong?

All Replies

  • Tuesday, February 28, 2012 6:05 PM
     
     

    Could you present some more of the code, where and how are you binding ?

    As it seems from your snippet, you are doing this in codebehind, if that is the cause, there is no reason for the binding since you can just set the property of the textbox directly.


    Developing is part of being a developer.

  • Tuesday, February 28, 2012 6:26 PM
     
      Has Code

    The class containing FilePath must implement INotifyPropertyChanged event and when value get set in FilePath property, PropertyChanged event should be raised. 

    Try following code

    public class JobItem : System.ComponentModel.INotifyPropertyChanged
        {
            private string filePath;
     
            public string FilePath 
            { 
                get
                {
                    return filePath;
                }
                set
                {
                    filePath = value;
                    OnPropertyChanged("FilePath");
                }
            }
     
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(thisnew System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        }


    Gaurav Khanna | Microsoft VB.NET MVP


  • Tuesday, February 28, 2012 6:59 PM
     
      Has Code
      public class SiteData : System.ComponentModel.INotifyPropertyChanged
        {
                    private Uri filepath;
            public SiteData()
            {
            }
            public string FilePath
            {
                get 
                {
                    if (filepath != null)
                        return filepath.LocalPath;
                    else
                        return "";
                }
                set 
                {
                    filepath = new Uri(value);
                    OnPropertyChanged("FilePath");
                }
            }
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        }

    I did what you said, but it still doesn't work.
  • Tuesday, February 28, 2012 7:40 PM
     
     Answered Has Code

    I found the problem.

    When I wrote

    Binding URLBinding = new Binding("FilePath");
    URLBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    txtURL.SetBinding(ContentProperty, URLBinding);

    I should have wrote

    Binding URLBinding = new Binding("FilePath");            
    txtURL.SetBinding(TextBox.TextProperty, URLBinding);

    • Proposed As Answer by SharpAspirant Tuesday, February 28, 2012 7:57 PM
    • Marked As Answer by Newmanb1 Tuesday, February 28, 2012 8:22 PM
    •  
  • Tuesday, February 28, 2012 7:57 PM
     
     
    Good to hear !

    Developing is part of being a developer.