I’m trying to insert some design data in a control by temporarily setting datacontext in xml. The contents are defined by the following class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using AITReportEditor.Infrastructure.Models;
using System.Collections.ObjectModel;
namespace AITReportEditor.Infrastructure.Models
{
public class ComponentItem : NotificationObject
{
//Constructor for expression Blend sample data
public ComponentItem() { }
public ComponentItem(AITReportEditor.Infrastructure.Models.Component copyItem)
{
this.PartData = new PartDataItem(copyItem.PartData);
this.ObjectId = copyItem.ObjectId;
Files = new ObservableCollection<FileItem>();
foreach (File existItm in copyItem.Files)
{
FileItem fileItem = new FileItem(existItm);
Files.Add(fileItem);
}
SpareParts = new ObservableCollection<SparepartItem>();
foreach (Sparepart existItm in copyItem.Spareparts)
{
SparepartItem sparepartItem = new SparepartItem(existItm);
SpareParts.Add(sparepartItem);
}
}
private ObservableCollection<FileItem> _files;
public ObservableCollection<FileItem> Files
{
get { return _files; }
set
{
_files = value;
}
}
private ObservableCollection<SparepartItem> spareparts;
public ObservableCollection<SparepartItem> SpareParts
{
get { return spareparts; }
set
{
spareparts = value;
}
}
private string _objectid;
public string ObjectId
{
get { return _objectid; }
set
{
if (!value.Equals(_objectid))
{
_objectid = value;
this.RaisePropertyChanged(() => this.ObjectId);
}
}
}
private PartDataItem _partdata;
public PartDataItem PartData
{
get { return _partdata; }
set
{
if (!value.Equals(_partdata))
{
_partdata = value;
this.RaisePropertyChanged(() => this.PartData);
}
}
}
}
}
and
namespace AITReportEditor.Infrastructure.Models
{
public class PartDataItem : NotificationObject
{
public PartDataItem() { }
public PartDataItem(InspectionObject copyItem)
{
this.Name = copyItem.Name;
this.Note = copyItem.Note;
this.Status = copyItem.Status;
}
private string _name;
public string Name
{
get { return _name; }
set
{
if (!value.Equals(_name))
{
_name = value;
this.RaisePropertyChanged(() => this.Name);
}
}
}
private string _note;
public string Note
{
get { return _note; }
set
{
if (!value.Equals(_note))
{
_note = value;
this.RaisePropertyChanged(() => this.Note);
}
}
}
private int _status;
public int Status
{
get { return _status; }
set
{
if (!value.Equals(_status))
{
_status = value;
this.RaisePropertyChanged(() => this.Status);
}
}
}
}
}
In xaml i define datacontext with
xmlns:inf="clr-namespace:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure"
<UserControl.DataContext>
<inf:ComponentItem>
<inf:ComponentItem.ObjectId>1212</inf:ComponentItem.ObjectId>
<inf:ComponentItem.PartData>
<inf:ComponentItem.PartData.Name>klas</inf:ComponentItem.PartData.Name>
</inf:ComponentItem.PartData>
</inf:ComponentItem>
</UserControl.DataContext>
The ObjectId works fine, but I can’t seem to define the partdata. Anyone know what I’m doing wrong?
[UPDATE] Made some changes
Instead of defining sampledata insiede usercontrol datacontext I’m setting design data in the external file and changing xaml to
<Grid Background="Transparent" d:DataContext="{d:DesignDataSource=./../../SampleData/SampleData.xaml}">
in the control and defining sample data in seperate file
<m:ComponentItem xmlns:m="clr-namespace:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure"
ObjectId="12321">
<m:ComponentItem.PartData Name="Klas" />
</m:ComponentItem>
But I’m still getting “Cannot set property on element…” when I try to add PartData to sample file.
Anyone?
THink i solved it, seems to be all in the syntax for typed classes probaly obscurely hidden away in yet another book i haven’t read. To solve it i made a simpler projet with 3 classes Person, Invoice and ContactInfo
The syntax for the design data that works is then
Notice that if you want to use the list invoices you have to add
in the class.
Hope this could be of value to someone else.