我試圖通過在xml中臨時設置datacontext來在控件中插入一些設計數據。內容通過下面的類定義:WPF設計數據
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);
}
}
}
}
}
和
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);
}
}
}
}
}
在XAML我定義的DataContext與
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>
的的ObjectId工作正常,但我似乎無法定義該部分數據。任何人都知道我在做什麼錯了?
[UPDATE]做了一些改變
代替限定的sampleData insiede用戶控件的datacontext我設置在外部文件的設計數據和在控制改變XAML到
<Grid Background="Transparent" d:DataContext="{d:DesignDataSource=./../../SampleData/SampleData.xaml}">
並限定樣本數據單獨的文件
<m:ComponentItem xmlns:m="clr-namespace:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure"
ObjectId="12321">
<m:ComponentItem.PartData Name="Klas" />
</m:ComponentItem>
但我仍然得到「無法設置屬性元素上......」當我嘗試添加PartData採樣文件。
有人嗎?
感謝您的回答沒有工作,雖然,同樣的問題。 Visaual Studio抱怨「無法在...上設置屬性」。我做了一些更改並定義了一個外部文件,並使用「{d:DesignData Source = SampleData.xaml}」而不是datacontext。我會做出修改以解釋這個問題。 – klashagelqvist