清除绑定到wpf中静态属性的文本框
本文关键字:属性 文本 静态 绑定 wpf 清除 | 更新日期: 2024-10-23 20:50:15
假设我在WPF Page
中有一个textbox
。
该textbox
绑定到在MainWindowViewModel
中声明的static property
。此外,该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 = 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”