winforms databinding works only on development machine

匆匆过客 提交于 2019-12-02 02:53:59

问题


I'm targeting the framework 4.0 and this works fine on the development machine, I can see at startup the form with the textbox displaying binded message in it. But when I deploy the executable on any other machine it won't work claiming that "cannot bind to the property or column Note on the DataSource". The very strange thing is that it works well on any machine if I compile targeting the 3.5 framework. Someone could explain this odd behaviour? What can be different between development environment and client machines?

namespace Demo
{
    public partial class Form1 : Form
    {
        private readonly SimpleDataContext _dataContext;

        public Form1()
        {
            InitializeComponent();

            _dataContext = new SimpleDataContext { Prop = new SimpleProp { Note = "hi!" }};
            textBox1.DataBindings.Add("Text", _dataContext, "Prop.Note");         
        }
    }

    public class SimpleDataContext
    {
        public SimpleProp Prop { get; set; }
    }

    public class SimpleProp
    {
        public string Note { get; set; }
    }  
}

回答1:


Breaking change in 4.0. See .Net 4.0 simple binding issue

The work around is to use a BindingSource:

public Form1() {
  InitializeComponent();
  _dataContext = new SimpleDataContext { Prop = new SimpleProp { Note = "hi!" } };
  BindingSource bs = new BindingSource(_dataContext, null);
  textBox1.DataBindings.Add("Text", bs, "Prop.Note");    
}


来源:https://stackoverflow.com/questions/11023682/winforms-databinding-works-only-on-development-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!