简体   繁体   English

C# winforms 数据绑定文本框到 wcf 请求

[英]C# winforms Data bind textboxes to wcf request

I'm currently trying to parse a request from my Customer-Class into a winform.我目前正在尝试将来自我的 Customer-Class 的请求解析为一个 winform。 I am getting the content through a WCF service i've built.我正在通过我构建的 WCF 服务获取内容。

Here is the Form1 code:这是Form1代码:

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btn_search_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();  
        int valueParsed;
        if(txt_KNr.TextLength == 6)
        {
            if(Int32.TryParse(txt_KNr.Text.Trim(), out valueParsed))
            {
                Service svc = new Service();
                WcfServiceGVO.CustomerData data = svc.Connect(txt_KNr.Text);
                dict = svc.SplitData(data);
                txt_FirstName.DataBindings.Add("Text", data, "FirstName", false, DataSourceUpdateMode.OnPropertyChanged);
                txt_LastName.DataBindings.Add("Text", data, "LastName");
                txt_Street.DataBindings.Add("Text", data, "Street");
                txt_PLZ.DataBindings.Add("Text", data, "PLZ");
                txt_Location.DataBindings.Add("Text", data, "Location");
                lbl_ampErg.DataBindings.Add("Text", data, "Ampel");
            }}

Here is part of the Reference.cs这是Reference.cs的一部分

        public string LastName {
        get {
            return this.LastNameField;
        }
        set {
            if ((object.ReferenceEquals(this.LastNameField, value) != true)) {
                this.LastNameField = value;
                this.RaisePropertyChanged("LastName");
            }
        }
    }

It works the first time, but the second time the compiler says there are 2 bindings (so i guess my DataBindings shouldn't be in the button_Click but under InitializeComponent();) If i do that, the code runs but won't update the textboxes.它第一次工作,但第二次编译器说有 2 个绑定(所以我猜我的 DataBindings 不应该在 button_Click 中,而是在 InitializeComponent(); 下)如果我这样做,代码运行但不会更新文本框。 What do i need to change, so that the textboxes get updated as soon as my request gets the data from the server?我需要更改什么,以便在我的请求从服务器获取数据后立即更新文本框? If the property is changed, it should update the interface.如果属性改变,它应该更新接口。

Option 1 - Quick Fix选项 1 - 快速修复

A quick fix would be clearing DataBindings collection before adding a new Binding .一个快速的解决方法是在添加新的Binding之前清除DataBindings集合。

Option 2 - Using BindingSource in Code选项 2 - 在代码中使用 BindingSource

You can create a BindingSource in code and set type of your model as it's DataSource , then perform data binding to the BindingSource in code.您可以在代码中创建一个BindingSource并将模型的类型设置为DataSource ,然后在代码中对BindingSource执行数据绑定。 Then when loading data, just assign data to DataSource property of the binding source.然后在加载数据时,只需将数据分配给绑定源的DataSource属性即可。 For example:例如:

private BindingSource bindingSource1 = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
    bindingSource1.DataSource = typeof(Model1);
    this.textBox1.DataBindings.Add("Text", bindingSource1, "Property1", true,
                                    DataSourceUpdateMode.OnValidation);
    this.textBox2.DataBindings.Add("Text", bindingSource1, "Property2", true,  
                                    DataSourceUpdateMode.OnValidation);
}

private void button1_Click(object sender, EventArgs e)
{
    bindingSource1.DataSource = GetData();
}

Option 3 - Designer Support选项 3 - 设计师支持

You can use a BindingSource component for data binding at design-time.您可以在设计时使用BindingSource组件进行数据绑定。 You can put a BindingSource component on your form and set it's data source property at design-time to the desired type.您可以在表单上放置一个BindingSource组件,并在设计时将其数据源属性设置为所需的类型。 Then you will have design-time support for data binding using designer.然后,您将使用设计器获得对数据绑定的设计时支持。 You can bind your text boxes and labels to that binding source.您可以将文本框和标签绑定到该绑定源。 Then when loading data, it's enough to assign data to DataSource property of BindingSource .然后在加载数据时,将数据分配给BindingSource DataSource属性就足够了。

More Information:更多信息:

The Problem i had was to understand that i could drag and drop the textboxes into the UI and how to actually do stuff with it.我遇到的问题是要了解我可以将文本框拖放到 UI 中,以及如何实际使用它。 Now it seems to be working because i've added the following code to my previous:现在它似乎可以工作了,因为我在之前的代码中添加了以下代码:

   private void btn_search_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();  
        int valueParsed;
        if(txt_KNr.TextLength == 6)
        {
            if(Int32.TryParse(txt_KNr.Text.Trim(), out valueParsed))
            {
                Service svc = new Service();
                WcfServiceGVO.CustomerData data = svc.Connect(txt_KNr.Text);
                customerDataBindingSource.DataSource = data;
                //txt_FirstName.DataBindings.Add("Text", data, "FirstName");
                //txt_LastName.DataBindings.Add("Text", data, "LastName");
                //txt_Street.DataBindings.Add("Text", data, "Street");
                //txt_PLZ.DataBindings.Add("Text", data, "PLZ");
                //txt_Location.DataBindings.Add("Text", data, "Location");
                //lbl_ampErg.DataBindings.Add("Text", data, "Ampel");                    
            }   
}

the key was to understand that i could just use my servicedata as a DataSource!关键是要明白我可以只使用我的 servicedata 作为数据源! Thanks Reza for the great help!感谢 Reza 的大力帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM