【问题标题】:Designdata with xaml and class带有 xaml 和类的设计数据
【发布时间】:2011-12-09 15:38:42
【问题描述】:

我有一个类 ComponentItem,我在下面简化了(伪代码)

public class ComponentItem : NotificationObject
{
    private ObservableCollection<FileItem> _files;
    public ObservableCollection<FileItem> Files

    private ObservableCollection<SparepartItem> spareparts;
    public ObservableCollection<SparepartItem> SpareParts
    public string ObjectId
}

如果我为这个类创建设计数据,我可以这样做

<m:ComponentItem xmlns:m="clr-namespace:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure" ObjectId="AAAA-BBBB-CCCC-DDDDD">
    <m:ComponentItem.PartData>
        <m:PartDataItem Name="Component name" Status="1" Note="This is a note description"  />   
    </m:ComponentItem.PartData>
    <m:ComponentItem.Files>
        <m:FileItem FileName="Image1.jpg" Note="This is a note1" ObjectId="AAAA-BBBB-CCCC-DDDDD" ></m:FileItem>
        <m:FileItem FileName="Image1.jpg" Note="This is a note2" ObjectId="AAAA-BBBB-CCCC-DDDDE" ></m:FileItem>
    </m:ComponentItem.Files>
    <m:ComponentItem.SpareParts>
        <m:SparepartItem ObjectId="AAAA-BBBB-CCCC-DDDDD" Note="This is a note1" SpId="1" Amount="22" />
        <m:SparepartItem ObjectId="AAAA-BBBB-CCCC-DDDDE" Note="This is a note2" SpId="1" Amount="23" />
    </m:ComponentItem.SpareParts>
</m:ComponentItem>

我可以将其用作用户控件的设计数据。用户控件包含自定义子用户控件,并且在我设计父控件时设计数据不会显示在子控件中,因此我想将设计数据单独添加到子控件中。我的问题是我似乎无法为像

这样的集合定义 xaml

私有 ObservableCollection _files;

似乎没有办法声明类似,伪 xaml 代码

<List<FileItem>  ............
 <m:FileItem ...... />

我通过声明一个不必要的类 FilesItem 解决了这个问题

 public class FilesItem : ObservableCollection<FileItem>
    {
        public FilesItem()
        {

        }
    }
}

和 xaml 文件。

<m:FilesItem xmlns:m="clr-namespace:AITReportEditor.Infrastructure.Models;assembly=AITReportEditor.Infrastructure">
    <m:FileItem FileName="Image1.jpg" Note="This is a note1" ObjectId="AAAA-BBBB-CCCC-DDDDD" ></m:FileItem>
    <m:FileItem FileName="Image2.jpg" Note="This is a note2" ObjectId="AAAA-BBBB-CCCC-DDDDE" ></m:FileItem>
    <m:FileItem FileName="Image2.jpg" Note="This is a note2" ObjectId="AAAA-BBBB-CCCC-DDDDF" ></m:FileItem>
</m:FilesItem>

任何人都知道这是否是这样做的方法,或者我可以以某种方式摆脱不必要的课程。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您不能在 XAML 中创建通用实例(至少在已编译的 XAML 中),一种解决方法是子类化,如您所示。另一种方法是将创建委托给标记扩展,如下所示:

    [ContentProperty("Items")]
    public class GenericCollectionFactoryExtension : MarkupExtension
    {
        public Type Type { get; set; }
    
        private readonly List<Type> _TypeArguments = new List<Type>();
        public List<Type> TypeArguments { get { return _TypeArguments; } }
    
        private readonly List<Object> _Items = new List<Object>();
        public List<Object> Items { get { return _Items; } }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var genericType = Type.MakeGenericType(_TypeArguments.ToArray());
            var list = Activator.CreateInstance(genericType) as IList;
            if (list == null) throw new Exception("Instance type does not implement IList");
            foreach (var item in Items)
            {
                list.Add(item);
            }
            return list;
        }
    }
    

    这会使用反射创建通用集合,添加传递给它的所有项目(这里假设将创建一个 IList 以使其更容易)并返回集合。

    用法:

    xmlns:om="clr-namespace:System.Collections.ObjectModel;assembly=System"
    
    <me:GenericCollectionFactory x:Key="files"
                    Type="{x:Type om:ObservableCollection`1}">
        <me:GenericCollectionFactory.TypeArguments>
            <x:Type Type="m:FileItem" />
        </me:GenericCollectionFactory.TypeArguments>
        <m:FileItem FileName="Image1.jpg" Note="This is a note1" ObjectId="AAAA-BBBB-CCCC-DDDDD" />
        <m:FileItem FileName="Image1.jpg" Note="This is a note2" ObjectId="AAAA-BBBB-CCCC-DDDDE" />
    </me:GenericCollectionFactory>
    <!-- ... -->
    <m:ComponentItem Files="{StaticResource files}"/>
    

    `1 是指定此类型为通用类型所必需的。您可以通过创建一个只接受一个类型参数的版本来减少 XAML(就像ObservableCollection 这样的许多类只有一个),节省三行。

    【讨论】:

    • 很高兴能帮上忙,我和这个打得很厉害。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2011-10-25
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多