简体   繁体   English

UserControl 中的数据绑定在设计时不起作用?

[英]DataBinding within a UserControl not working at design time?

I have a small question regarding databinding and user controls.我有一个关于数据绑定和用户控件的小问题。

I construct (using C# 2010) a user control which is basically a wrapper for a ComboBox, and it has a custom property, which when changed, sets the value of the ComboBox.我构造(使用 C# 2010)一个用户控件,它基本上是 ComboBox 的包装器,它有一个自定义属性,当更改时,设置 ComboBox 的值。 Conversely, should the selected item in the ComboBox change, the value of the property is changed.相反,如果 ComboBox 中的选定项发生变化,则该属性的值也会发生变化。

Now, I could do this by trapping the "selected value changed" event on the ComboBox, and setting the property, and I could, in the property setter, set the selected value of the ComboBox, but I surmised I might also be able do this with DataBinding.现在,我可以通过在 ComboBox 上捕获“selected value changed”事件并设置属性来做到这一点,我可以在属性设置器中设置 ComboBox 的选定值,但我推测我也可以做到这与数据绑定。

And it nearly works, but not quite.它几乎有效,但不完全有效。

It works at run-time, but not at design time and I was wondering if this could be easilly resolved.它在运行时有效,但在设计时无效,我想知道这是否可以轻松解决。

For example, if at design time, I select the instance of my user control, and from the properties window select my control's custom property, and change it, the ComboBox does not reflect the change.例如,如果在设计时,我选择了我的用户控件的实例,并从属性窗口中选择了我的控件的自定义属性并对其进行了更改,则 ComboBox 不会反映更改。

Any pointers to something I have missed would be greatfully received.任何指向我错过的东西的指针都会受到极大的欢迎。 Obviously, I could set the ComboBox selected value, but it would be nice if DataBinding would do it for me.显然,我可以设置 ComboBox selected 值,但如果 DataBinding 为我做这件事会很好。

Thanks谢谢

(Here is my user control. Drop one on a form and use the IDE to change the "Position" property) (这是我的用户控件。在窗体上放置一个并使用 IDE 更改“位置”属性)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
  public partial class UserControl1 : UserControl, INotifyPropertyChanged
  {
     public event PropertyChangedEventHandler PropertyChanged;

     public enum enumPosition : byte
     {
        Unknown, First, Second, Third
     }

     public UserControl1()
     {
        InitializeComponent();

        var bindingList = new BindingList<KeyValuePair<enumPosition, String>>();

        foreach (enumPosition value in Enum.GetValues(typeof(enumPosition)))
        {
           bindingList.Add(new KeyValuePair<enumPosition, String>(value, value.ToString()));
        }

        this.comboBox1.DisplayMember = "Value";
        this.comboBox1.ValueMember = "Key";
        this.comboBox1.DataSource = bindingList;

        this.comboBox1.DataBindings.Add("SelectedValue", this, "Position", false, DataSourceUpdateMode.OnPropertyChanged);
     }

     private enumPosition _position = enumPosition.Unknown;

     [DefaultValue(typeof(enumPosition), "Unknown")]
     public enumPosition Position
     {
        get { return _position; }
        set
        {
           if (value != _position)
           {
              _position = value;
              this.OnPropertyChanged(new PropertyChangedEventArgs("Position"));
           }
        }
     }

     private void OnPropertyChanged(PropertyChangedEventArgs e)
     {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
     }
  }
}

Works for me too !也适用于我! Environment - VS .Net 2008环境 - VS .Net 2008

Only difference I think might be 'Re-Building' the application instead of just 'Build' ?我认为唯一的区别可能是“重新构建”应用程序,而不仅仅是“构建”?

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

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