如果我无法创建属性,我如何绑定到字段

时间:2013-09-02 10:47:09

标签: c# wpf xml

我正在尝试将字段绑定到控件上,我正在生成这样的:

private void GenerateForm<T>() where T : new()
    {
        var type = typeof(T);

        InputGrid.Children.Clear();
        int i = 0;

        foreach (var property in type.GetFields(Flags))
        {
            InputGrid.RowDefinitions.Add(AutoSizeRowDefinition);
            var textBlock = new TextBlock { Text = property.Name, Margin = _margin };
            Grid.SetRow(textBlock, i++);
            InputGrid.Children.Add(textBlock);

            InputGrid.RowDefinitions.Add(AutoSizeRowDefinition);
            var control = GetControl(property);
            Grid.SetRow(control, i++);
            InputGrid.Children.Add(control);
        }

        InputGrid.RowDefinitions.Add(AutoSizeRowDefinition);
        var button = new Button { Content = "Отправить", Margin = _margin, MinHeight = 60};
        button.Click += GetButtonClick<T>();
        Grid.SetRow(button, i);
        InputGrid.Children.Add(button);
    }

所以我无法创建属性,因为它是“添加引用”自动生成的类,它们生成字段,而不是属性

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18058")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://api.service.soap.emias.lanit.ru/")]
public partial class appointmentReceptionToDoctor : object, System.ComponentModel.INotifyPropertyChanged {

private string doctorSpecialityCodeField;

private string doctorSpecialityField;

... and so on

那么为了所有这些东西,我该如何绑定它们呢?可能在运行时使用名为fields的属性生成类,并在将它们转换回来之后,但它似乎很复杂。

已添加:某些类是使用属性正确生成的,而某些类是使用公共字段生成的。我不知道为什么。我正在尝试重新映射它们,但现在远程不可用。

我这样添加:image

我得到这样的课程

    [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="getLpusInfo", WrapperNamespace="http://api.service.soap.emias.lanit.ru/", IsWrapped=true)]
public partial class getLpusInfo {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=0)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string omsNumber;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=1)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string omsSeries;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=2)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public System.DateTime birthDate;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=3)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string lpuTypeCode;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=4)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string lpuName;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=5)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string baseLpuAddress;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=6)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Lanit.Emias.RemoteServices.Appointment.PGUServicesInfo.serviceLpuAddress serviceLpuAddress;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://api.service.soap.emias.lanit.ru/", Order=7)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string externalSystemId;

    public getLpusInfo() {
    }

    public getLpusInfo(string omsNumber, string omsSeries, System.DateTime birthDate, string lpuTypeCode, string lpuName, string baseLpuAddress, Lanit.Emias.RemoteServices.Appointment.PGUServicesInfo.serviceLpuAddress serviceLpuAddress, string externalSystemId) {
        this.omsNumber = omsNumber;
        this.omsSeries = omsSeries;
        this.birthDate = birthDate;
        this.lpuTypeCode = lpuTypeCode;
        this.lpuName = lpuName;
        this.baseLpuAddress = baseLpuAddress;
        this.serviceLpuAddress = serviceLpuAddress;
        this.externalSystemId = externalSystemId;
    }
}

或者像这样:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18058")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://api.service.soap.emias.lanit.ru/")]
public partial class serviceLpuAddress : baseDto {

    private string areaField;

    private string districtField;

    private string streetField;

    private string houseField;

    private string buildingField;

    private string constructionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public string area {
        get {
            return this.areaField;
        }
        set {
            this.areaField = value;
            this.RaisePropertyChanged("area");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
    public string district {
        get {
            return this.districtField;
        }
        set {
            this.districtField = value;
            this.RaisePropertyChanged("district");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)]
    public string street {
        get {
            return this.streetField;
        }
        set {
            this.streetField = value;
            this.RaisePropertyChanged("street");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
    public string house {
        get {
            return this.houseField;
        }
        set {
            this.houseField = value;
            this.RaisePropertyChanged("house");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=4)]
    public string building {
        get {
            return this.buildingField;
        }
        set {
            this.buildingField = value;
            this.RaisePropertyChanged("building");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=5)]
    public string construction {
        get {
            return this.constructionField;
        }
        set {
            this.constructionField = value;
            this.RaisePropertyChanged("construction");
        }
    }
}

我不知道为什么他们不同:(

2 个答案:

答案 0 :(得分:1)

确定没有属性吗?因为私人领域不是很有用; web-references(无论是基于wsdl还是基于svcutil)通常都会添加属性。

但是,如果它真的没有,那么因为它是partial class你仍然可以:在第二个代码文件中,在正确的命名空间中:

namespace Get.This.Right
{
    partial class appointmentReceptionToDoctor {
        public string DoctorSpecialityCode {
            get { return doctorSpecialityCodeField; }
            set { doctorSpecialityCodeField = value; }
        }

        // etc
    }
}
然而,坦率地说,我发现生成的类型没有具有属性而且只有私有字段非常奇怪 - 它几乎无法使用。事实上,INotifyPropertyChanged(它实现的)似乎没有意义,如果它还没有属性。您确定生成的代码中还没有DoctorSpecialityDoctorSpecialityCode吗?

答案 1 :(得分:0)

解决这个问题的常用方法是创建一个'wrapper'类来包装你生成的类,或者只是创建一个具有相同属性的正常类 plus 你需要的额外属性然后你只需要迭代你可能拥有的任何集合,并将生成的类实例中的值复制到新的类实例中。