简体   繁体   中英

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. Conversely, should the selected item in the ComboBox change, the value of the property is changed.

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.

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.

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.

Thanks

(Here is my user control. Drop one on a form and use the IDE to change the "Position" property)

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

Only difference I think might be 'Re-Building' the application instead of just 'Build' ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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