【问题标题】:Value of the property does not change when the textbox is cleared清除文本框时属性的值不会改变
【发布时间】:2013-12-14 14:06:13
【问题描述】:

我有一个名为 HaemogramViewModel 的 ViewModel

代码如下:

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;

    }
}

在我的 MainWindowViewModel 类中:

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);
                }
            }
        }
    }   
}

我的文本框绑定到 CurrentHaemogram 属性的血红蛋白字段。

当我在文本框中输入一些值然后单击保存按钮时,一切正常。

现在的问题是:

当我在文本框中输入一些值时,我按 Tab 键,然后再次单击文本框,然后清除文本框中的值。现在,如果我点击保存按钮,那么我不会得到文本框的值 = null,而是得到文本框的值 = 我之前输入的值。

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    试试这个,效果很好

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

    在您看来,模型属性应声明如下

     private double? b;
        public double? B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("B"));
                }
            }
        }
    

    【讨论】:

    • 谢谢,这按预期工作。现在我知道TargetNullValue 的重要性了。
    【解决方案2】:

    在您的 xmal 中,您必须如下设置属性 UpdateSourceTrigger=PropertyChanged

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

    默认UpdateSourceTrigger=LostFocus,这意味着绑定到文本框的属性会在你按下Tab键或者失去焦点后更新。如果您设置为 PropertyChanged,它将为 textBox 中的每个字符更改更新属性

    【讨论】:

    • 我也试过了,但它不起作用。我的意思是如果我输入 15 然后我使用退格键删除它然后在保存时我也会得到 cHaemogram.Haemoglobin = 1 而不是 cHaemogram.Haemoglobin = null;
    • 哦,这意味着你绑定了 int 属性,而不是字符串。如果将 int 属性绑定到 TextBox,则当所有内容都被清除时,TextBox 中的值会因为 null 而无法转换为 int,因此 textBox 不会更新您的属性。请将您的属性设置为字符串并进行验证
    • 不,我已将我的文本框绑定到双倍?,即可以为空的双倍。因此,它应该会按预期更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多