I have a report object (i.e. a business object) which has several dozen fields to populate. Each field by itself has INotifyPropertyChanged
implemented. There is an accessor property for the active report called ActiveReport
.
What I want to do is be able to Close the current report, without necessarily opening a new one, and be able to automatically create a report object when the user starts to enter data again.
Here is a rough idea of the structure. ActiveReport
is the current report. The GUI is able to directly set the fields of the subclass (name/email) through binding. I want a new BusinessObject
to be created when name is being set, but ActiveReport is null. One additional caveat, the report object is auto-generated from XSD files, so I'd rather not have to modify those.
class ControlClass {
public BusinessObject ActiveReport { get; set; }
}
class BusinessObject {
UserInfo field1 { get; set; }
}
class UserInfo : INotifyPropertyChanged {
DependencyProperty name;
DependencyProperty email;
}
I开发者_如何转开发 thought of the following scenarios:
- Accessor property.
- The binding does not seem to use the accessor.
- Inserting a check into all event handlers.
- I'd rather not have to resort to this -- this breaks the rationale behind using MVVM.
- Multibinding
- This would require the use of a converter class and instance, and that seems like overkill.
- Converter
I thought to ask if there were any other good programming models for this in WPF.
You could create a behavior. in it, you check if (AssociatedObject.DataContext as ReportObject) is null and if it is, clear all your fields / set your datacontext / whatever
This should do the trick:
public class ControlClass
{
public BusinessObject ActiveReport { get; set; }
private UserInfo _editableUserData
public UserInfo EditableUserData
{
get { return _editableUserData; }
set
{
if (_editableUserData != null)
_editableUserData.PropertyChanged -= UserDataChanged;
_editableUserData = value;
if (_editableUserData != null)
_editableUserData.PropertyChanged += UserDataChanged;
RaisePropertyChanged("EditableUserData");
}
}
private void UserDataChanged(object sender, PropertyChangedEventArgs e)
{
if (ActiveReport == null)
ActiveReport = new BusinessObject(EditableUserData);
}
}
精彩评论