我在WPF应用程序中的ViewModel遇到问题。当值不为空时,它确实会更改PullingOrganization的属性,但是当值为空时,它将保留原始值。它到达设置器,但由于某些原因,值未更新。
XAML页面:
<Label Grid.Row="0" Grid.Column="0" Content="Naam van organisatie" Style="{StaticResource LabelInputField}"/>
<TextBox Grid.Row="0" Grid.Column="1" Name="inputOrganizationName" Text="{Binding OrganizationName}" Margin="0 5"/>
特定的viewModel类:
public class OrganizationAddWindowModel : BaseViewModel
{
private PullingOrganization organization;
public string OrganizationName
{
get { return organization.Name; }
set
{
if (value != organization.Name)
{
organization.Name = value;
OnPropertyChange("OrganizationName");
}
}
}
public string OrganizationLogo
{
get { return organization.Logo; }
set
{
if (value != organization.Logo)
{
organization.Logo = value;
OnPropertyChange("OrganizationLogo");
}
}
}
public PullingOrganization Organization
{
get { return this.organization; }
}
public OrganizationAddWindowModel()
{
WindowTitle = "Truckpulling - Nieuwe organisatie";
organization = new PullingOrganization();
}
}
一般视图模型类。
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
//PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string windowTitle;
public string WindowTitle
{
get { return windowTitle; }
set
{
if(windowTitle != value)
{
windowTitle = value;
OnPropertyChange("WindowTitle");
}
}
}
}
View和viewmodel之间的连接是这样的:
public partial class AddWindow : Window
{
private OrganizationAddWindowModel _viewModel;
public AddWindow()
{
InitializeComponent();
_viewModel = new OrganizationAddWindowModel();
DataContext = _viewModel;
}