Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清除绑定到wpf中静态属性的文本框_C#_Wpf - Fatal编程技术网

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

C# 清除绑定到wpf中静态属性的文本框,c#,wpf,C#,Wpf,假设我在WPF页面中有一个textbox 此文本框绑定到在MainWindowViewModel中声明的静态属性。此外,此静态属性实现了INotifyPropertyChanged 以下是MainWindowViewModel的代码: namespace WpfApplication1.ViewModels { class MainWindowViewModel : INotifyPropertyChanged { public MainWindowViewMo

假设我在
WPF页面中有一个
textbox

文本框
绑定到在
MainWindowViewModel
中声明的
静态属性
。此外,此
静态属性
实现了
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));
            }
        }
    }
}
我在上面代码的注释中提到了我的问题。不过,我想在这里提一提:

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

当我说
cPerson=newperson()时
Event
OnStaticPropertyChanged
被引发,并且看起来一切正常。当我调试时,我得到
cPerson.Name=null
cPerson.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>

尝试如下设置目标空值

TargetNullValue={x:Static System:String.Empty}
还要将此名称空间添加到XAML中

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

您可以从Person类处理它。当Age为null时,可以在Age getter中返回string.Empty。名称也一样。但名称也不适用。下面是我创建的一个小项目的链接,该项目的绑定属性名称、年龄未实现属性更改。CurrentPerson实现INotifyPropertyChanged。所以,我认为姓名和年龄是自动实现的。我的Person类是由实体框架自动生成的。现在你能给我举一个你第一次评论的例子吗?
xmlns:System=”clr-namespace:System;assembly=mscorlib”