Value of the property does not change when the tex

2019-06-06 09:09发布

I have a ViewModel Called HaemogramViewModel

Here is code:

public class HaemogramViewModel : INotifyPropertyChanged
    {
        public HaemogramViewModel()
        {

        }

        public Haemogram CurrentHaemogramReport
        {
            get
            {
                return MainWindowViewModel.cHaemogram;
            }
            set
            {
                MainWindowViewModel.cHaemogram = value;
                OnPropertyChanged("CurrentHaemogramReport");
            }
        }


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

        public event PropertyChangedEventHandler PropertyChanged;

    }
}

In my MainWindowViewModel Calss:

class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
            cHaemogram = new Haemogram();
    }

    public static Haemogram cHaemogram { get; set; }

    private void SaveChanges(object obj)
    {
        using (Lab_Lite_Entities db = new Lab_Lite_Entities())
        {

            //db.Patients.Add(CurrentPatient);

            if (cHaemogram != null)
            {
                if (cHaemogram.Haemoglobin != null)
                {
                    db.Haemograms.Add(cHaemogram);
                }
            }
        }
    }   
}

My textbox is bound to the field Haemoglobin of CurrentHaemogram Property.

When I enter some value in the textbox and then click save button then everything works fine.

Now the problem is :

When I enter some value in the textbox then I press tab and then again click on textbox and then clear the value in the textbox. Now if I click on save button then I don't get the textbox's value = null, instead I get the textbox's value = the value that I entered previously.

标签: c# wpf mvvm
2条回答
男人必须洒脱
2楼-- · 2019-06-06 10:02

In your xmal you have to set the property UpdateSourceTrigger=PropertyChanged as below

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

By defalut UpdateSourceTrigger=LostFocus, that means the property bound to the textBox will get updated once you press tab or it's focus is lost. If you set to PropertyChanged it will update the property for every char change in the textBox

查看更多
冷血范
3楼-- · 2019-06-06 10:14

Try this it works

<TextBox Text="{Binding B, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}"/>

and in you view model property should be declared as below

 private double? b;
    public double? B
    {
        get
        {
            return b;
        }
        set
        {
            b = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("B"));
            }
        }
    }
查看更多
登录 后发表回答