Reputation: 1759
I'm programming a WPF-window-designer application. In the designer i can add customcontrols to the window and save the window by serializing the Canvas panel on which the added customcontrols are lying to XAML.
public string SerializeControlToXaml(FrameworkElement control)
{
StringBuilder outstr = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
XamlDesignerSerializationManager dsm =
new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
dsm.XamlWriterMode = XamlWriterMode.Expression;
System.Windows.Markup.XamlWriter.Save(control, dsm);
string xaml = outstr.ToString();
return xaml;
}
After that serialization i get the xaml as string and save it in a file which i can reload later.
Now my problem is that i added a new dependency property to one of my customcontrols which is of type ObservableCollection. Each time i have set that property and try to serialize the canvas to XAML i get an error message:
"Invalid initials in 'ObservableCollection 1'. The sign '
', hexadecimal value 0x60, may not be contained in a name."
I never give the collection a name. What's going wrong?
This is the viewmodel-property to which i bind the control:
public ObservableCollection<string> SelectedFormulas
{
get
{
return selectedFormulas;
}
set
{
selectedFormulas = value;
RaisePropertyChanged("SelectedFormulas");
}
}
And this is my dependencyproperty:
public static readonly DependencyProperty SelectedFormulasProperty =
DependencyProperty.Register("SelectedFormulas", typeof(ObservableCollection<string>), typeof(CustomNumericField));
public ObservableCollection<string> SelectedFormulas
{
get { return GetValue(SelectedFormulasProperty) as ObservableCollection<string>; }
set { SetValue(SelectedFormulasProperty, value); }
}
Upvotes: 1
Views: 1236
Reputation: 1759
The answer from dbc is the solution.
What i needed was a non-generic class instead of the ObservableCollection.
This is the new class:
public class SelectedFormulaCollection : ObservableCollection<string>
{
}
And here the DependencyProperty in the customcontrol (Type of the property in the viewmodel must be changed too!):
public static readonly DependencyProperty SelectedFormulasProperty =
DependencyProperty.Register("SelectedFormulas", typeof(SelectedFormulaCollection), typeof(CustomNumericField));
public SelectedFormulaCollection SelectedFormulas
{
get { return GetValue(SelectedFormulasProperty) as SelectedFormulaCollection; }
set { SetValue(SelectedFormulasProperty, value); }
}
Upvotes: 3