Khushi
Khushi

Reputation: 1051

Clear a textbox which is bound to a static property in wpf

Suppose I have a textbox in a WPF Page.

This textbox is bound to a static property which is declared in MainWindowViewModel. Also this static property implements INotifyPropertyChanged.

Here is the code for MainWindowViewModel :

namespace WpfApplication1.ViewModels
{
    class MainWindowViewModel : INotifyPropertyChanged
    {

        public MainWindowViewModel()
        {
            cPerson = new Person();

            SaveChangesCommand = new RelayCommand(SaveChanges);
            MainWindowViewModel.StaticPropertyChanged += MainWindowViewModel_StaticPropertyChanged;
        }

        void MainWindowViewModel_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        private static Person _cPerson;
        public static Person cPerson
        {
            get
            {
                return _cPerson;
            }
            set
            {
                _cPerson = value;
                OnStaticPropertyChanged("cPerson");
            }
        }

        public ICommand SaveChangesCommand { get; set; }

        private void SaveChanges(object obj)
        {
            //code for saving <-- Gives me values back. i.e. It works fine
            using (SampleEntities db = new SampleEntities())
            {
                db.People.Add(cPerson);
                db.SaveChanges();
            }

            //clear all the fields <-- Here it calls OnStaticPropertyChanged But Fields are not cleared 
            cPerson = new Person();

        }

        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
        protected static void OnStaticPropertyChanged(string PropertyName)
        {
            EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
            if (handler != null)
            {
                handler(typeof(MainWindowViewModel), new PropertyChangedEventArgs(PropertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

Here is the code for Page1ViewModel:

namespace WpfApplication1.ViewModels
{
    class Page1ViewModel : INotifyPropertyChanged
    {

        public Page1ViewModel()
        {

        }

        public Person CurrentPerson
        {
            get
            {
                return MainWindowViewModel.cPerson;
            }
            set
            {
                MainWindowViewModel.cPerson = value;
                OnPropertyChanged("CurrentPerson");
            }
        }


        protected virtual void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}

I have mentioned my problem in the comments in above code. Still I would like to mention it here:

After I save changes to the database, I want to clear all the textboxes so that user can again fill in the new values.

When I say cPerson = new Person(); Event OnStaticPropertyChanged is raised and all looks fine. When I debug I get cPerson.Name = null and cPerson.Age = null. But when I see the textboxes the old values are not replaced with null.

I have set TargetNullValue = '' in my xaml.

If you like to see the xaml, then here it is:

<Page x:Class="WpfApplication1.Pages.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:vm="clr-namespace:WpfApplication1.ViewModels"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Page1">

    <Page.DataContext>
        <vm:Page1ViewModel />
    </Page.DataContext>

    <Canvas DataContext="{Binding DataContext.CurrentPerson, RelativeSource={RelativeSource AncestorType={x:Type Page}}}">
        <TextBlock Canvas.Left="10" TextWrapping="Wrap" Text="             Name :" Canvas.Top="44"/>
        <TextBox Height="23" Canvas.Left="96" TextWrapping="Wrap"
                 Text="{Binding Name, UpdateSourceTrigger=LostFocus, TargetNullValue=''}" Canvas.Top="41" Width="120"/>
        <TextBlock Canvas.Left="10" TextWrapping="Wrap" Text="             Age    :" Canvas.Top="82"/>
        <TextBox Height="23" Canvas.Left="96" TextWrapping="Wrap" Text="{Binding Age, UpdateSourceTrigger=LostFocus, TargetNullValue=''}" Width="120" Canvas.Top="80"/>
    </Canvas>

</Page>

Upvotes: 0

Views: 249

Answers (1)

Sampath
Sampath

Reputation: 1211

Try setting target null value as below.

TargetNullValue={x:Static System:String.Empty}

Also add this namespace to your XAML

xmlns:System=”clr-namespace:System;assembly=mscorlib”

Upvotes: 1

Related Questions