WPF设计数据

WPF设计数据,wpf,xaml,Wpf,Xaml,我试图通过在xml中临时设置datacontext,在控件中插入一些设计数据。内容由以下类定义: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.Prism.ViewModel; using AITReportEditor.Infrastructure.Models; using System.Collections.Ob

我试图通过在xml中临时设置datacontext,在控件中插入一些设计数据。内容由以下类定义:

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);
                }

            }
        }
    }
}
在xaml中,我使用

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>
xmlns:inf=“clr命名空间:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure”
1212
克拉斯
ObjectId工作正常,但我似乎无法定义partdata。有人知道我做错了什么吗

[更新]进行了一些更改

我在外部文件中设置设计数据,并将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添加到示例文件时,仍然会出现“无法设置元素上的属性…”


有人吗?

我认为问题在于PartData作为引用类型,没有可设置Name属性的实例。字符串是一种值类型,将自动创建。尝试:

private PartDataItem _partdata = new PartDataItem();
我希望您认识到设置设计时数据应该通过在控件上编写依赖属性来完成,因为这将允许您通过xaml中的属性在控件上设置设计时数据。DataContext用于设置动态数据(可在设计时使用,但并非真正的目的)


希望这能有所帮助。

我想我解决了这个问题,似乎都是关于类型化类的语法,很可能隐藏在另一本我没读过的书中。为了解决这个问题,我做了一个简单的projet,有3个类:Person、Invoice和ContactInfo

 public class Person
    {
        public Person()
        { }


        public string Name { get; set; }

        public List<Invoice> Invoices{get; set;}

        public ContactInfo ContactInfo { get; set; }

    }

     public class Invoice
        {
            public int Amount { get; set; }
    }

 public class ContactInfo
    {

        public ContactInfo()
        {

        }
        public string Adress { get; set; }
    }
公共类人物
{
公众人士()
{ }
公共字符串名称{get;set;}
公共列表发票{get;set;}
公共联系人信息联系人信息{get;set;}
}
公共类发票
{
公共整数金额{get;set;}
}
公共类联系人信息
{
公共联系人信息()
{
}
公共字符串地址{get;set;}
}
设计数据的有效语法如下所示

<m:Person xmlns:m="clr-namespace:WpfApplication2" Name="Klas">
    <m:Person.Invoices>
        <m:Invoice Amount="12" />
        <m:Invoice Amount="12" />
        <m:Invoice Amount="12" />
    </m:Person.Invoices>
    <m:Person.ContactInfo>
        <m:ContactInfo Adress="Lena"></m:ContactInfo>
    </m:Person.ContactInfo>
</m:Person>

请注意,如果要使用列表发票,必须添加

private List<Invoice> _invoices = new List<Invoice>();
        public List<Invoice> Invoices
        {
            get { return _invoices; }
            set { _invoices = value; }
        }
private List_invoices=new List();
公开发票清单
{
获取{返回_发票;}
设置{u=value;}
}
在课堂上


希望这对其他人有价值。

谢谢你的回答,但同样的问题没有解决。Visaual Studio抱怨“无法在…上设置属性”。我做了一些更改,定义了一个外部文件,并使用“{d:DesignData Source=SampleData.xaml}”而不是datacontext。我会在问题的开头作出解释。
private List<Invoice> _invoices = new List<Invoice>();
        public List<Invoice> Invoices
        {
            get { return _invoices; }
            set { _invoices = value; }
        }