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()时
EventOnStaticPropertyChanged
被引发,并且看起来一切正常。当我调试时,我得到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”