verwendetes Datenbanksystem: xml
Hi,
ich habe mithilfe des "Getting startet"-Eintrags der Catel Dokumentation ein kleines Programm erstellt. Mein View Model besitzt eine Property vom Typ
System.Windows.Media.Color
allerdings wird diese nicht mit dieser Methode gespeichert in der Xml-Datei gespeichert:
public void SavePersons(IEnumerable<Person> person)
{
Settings settings = new Settings();
settings.Persons.ReplaceRange(persons);
settings.Save(_path, SerializationMode.Xml);
}
Meine Settings Klasse sieht so aus:
public class Settings : SavableModelBase<Settings>
{
public static readonly PropertyData PersonsProperty = RegisterProperty("Persons",
typeof (ObservableCollection<Person>), () => new ObservableCollection<Person>());
public ObservableCollection<Person> Persons
{
get { return GetValue<ObservableCollection<Person>>(PersonsProperty); }
set { SetValue(PersonsProperty, value); }
}
}
Das ist das ViewModel:
public class PersonViewModel : ViewModelBase
{
public static readonly PropertyData PersonProperty = RegisterProperty("Person", typeof (Person));
public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof (string));
public static readonly PropertyData ColorProperty = RegisterProperty("Color", typeof (Color));
public PersonViewModel(Person person)
{
Argument.IsNotNull(() => person);
Person = person;
}
[Model]
public Person Person
{
get { return GetValue<Person>(PersonProperty); }
set { SetValue(PersonProperty, value); }
}
[ViewModelToModel("Person")]
public string Name
{
get { return GetValue<string>(NameProperty); }
set { SetValue(NameProperty, value); }
}
[ViewModelToModel("Person")]
public Color Color
{
get { return GetValue<Color>(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
}
Und das dazugehörige Model:
public class Person : ModelBase
{
public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof (string));
public static readonly PropertyData ColorProperty = RegisterProperty("Color", typeof (Color));
public string Name
{
get { return GetValue<string>(NameProperty); }
set { SetValue(NameProperty, value); }
}
public Color Color
{
get { return GetValue<Color>(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
public override string ToString()
{
string name = string.Empty;
if (!string.IsNullOrWhiteSpace(Name))
{
name += Name;
}
return name;
}
protected override void ValidateFields(List<IFieldValidationResult> validationResults)
{
if (string.IsNullOrWhiteSpace(Name))
{
validationResults.Add(FieldValidationResult.CreateError(NameProperty, "Der Name muss eingegeben werden."));
}
}
}