清除绑定到wpf中的静态属性的文本框

时间:2013-12-17 11:51:24

标签: c# wpf

假设我textbox中有WPF Page

textbox绑定到在static property中声明的MainWindowViewModel。此static property实现了INotifyPropertyChanged

以下是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));
            }
        }
    }
}

以下是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;

    }
}

我在上面代码的评论中提到了我的问题。我还想在这里提一下:

保存对数据库的更改后,我想清除所有文本框,以便用户可以再次填写新值。

当我说cPerson = new Person();事件OnStaticPropertyChanged被提出并且一切看起来都很好。当我调试时,我得到cPerson.Name = nullcPerson.Age = null。但是当我看到文本框时,旧值不会替换为null。

我在我的xaml中设置了TargetNullValue = ''

如果你想看xaml,那么它就是:

<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>

1 个答案:

答案 0 :(得分:1)

尝试设置目标空值,如下所示。

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

还要将此命名空间添加到您的XAML

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