0

I have a static singleton class with properties I use to databind textboxes to.

using System.ComponentModel;

namespace Masca
{
    public class logged : INotifyPropertyChanged
    {    
        public static logged instance = new logged();
        public static logged Instance
        {
            get { return instance; }
        }

        private string alisa;
        public string aliasname
        {
            get { return alisa; }
            set
            {
                alisa = value;
                RaisePropertyChanged("aliasname");
            }
        }

        private string mail;
        public string emailadd
        {
            get { return mail; }
            set
            {
                mail = value;
                RaisePropertyChanged("emailadd");
            }
        }

        private void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged (this, new PropertyChangedEventArgs(prop));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

This is the property access method:

loggedin.Instance.emailadd = "email.text";

This is the datacontext I place in the initialize components of all other pages I wish to access the property:

DataContext = loggedin.Instance;

and this is the XAML code for a bound TextBox

<TextBox x:Name="email" Text="{Binding emailadd}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" Margin="134,417,0,0"/>

<TextBox x:Name="mail" Text="{Binding emailadd}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" Margin="134,435,0,0"/>

The problem is that if I type something into email.text, mail.text will only reflect what is in email.text once i have clicked in mail.text.

Help is much appreciated.

Abbas
  • 14,186
  • 6
  • 41
  • 72
Offer
  • 630
  • 2
  • 11
  • 28

2 Answers2

2

Try setting UpdateSouceTrigger:

<TextBox x:Name="email" Text="{Binding emailadd, 
                               UpdateSourceTrigger=PropertyChanged, 
                               Mode=TwoWay}" />

Note that the default value of UpdateSourceTrigger for a TextBox.Text is LostFocus, while for many other properties, it is PropertyChanged.

King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    To think I spent all morning turning my project up side down trying to figure this out... Thanks King. – Offer Dec 04 '13 at 11:30
2

if you're trying to trigger the binding for each key stroke you will need to set the UpdateSourceTrigger.

<TextBox Name="itemNameTextBox"
    Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}" />

this SO answer describes some more scenarios (eg. filtering the key pressed) Bind TextBox on Enter-key press

Community
  • 1
  • 1
sambomartin
  • 6,663
  • 7
  • 40
  • 64