Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Winforms Usercontrol Databinding question

Winforms Usercontrol Databinding question

Scheduled Pinned Locked Moved C#
helpquestioncsharpwpfwinforms
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15011965
    wrote on last edited by
    #1

    I have a databinding issue with a numericupdown control bound to a bindinglist of objects. The updown control is located on a usercontrol. The controls can be loaded with values from a file located in a defaults xml file. The databinding works as it should upon loading the defaults file the first time. If during the session, I want to reload the defaults the databinding is lost. I can see that the values are loaded in correctly, but the numericupdn doesn't change value. here's a simplified version of my code to help explain. Object class:

    public class Garage :
    {

    public BindingList Drawer = new BindingList
    {
    new DrawerData(),
    new DrawerData()

    	};
    

    }

    public class DrawerData : INotifyPropertyChanged
    {
    private decimal _NumberOfTools;
    public event PropertyChangedEventHandler PropertyChanged;

    public decimal NumberOfTools
    	{
    		get { return \_NumberOfTools; }
    		set {
    			if (\_NumberOfTools != value)
    			{
    				\_NumberOfTools = value;
    
    				OnPropertyChanged("NumberOfTools");
    			}
    		}
    	}
    
    public event PropertyChangedEventHandler PropertyChanged;
    	public void OnPropertyChanged(\[CallerMemberName\] string PropertyName = "")
    	{
    		
    		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    		
    	}
    

    }

    Usercontrol class:

    public partial class UcDrawer : UserControl, INotifyPropertyChanged
    {

    private readonly Garage \_Garage;
    private readonly int Drw
    BindingSource bindingSource = new BindingSource();
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    public UcDrawer(int drw,Garage garage)
    	{
    		\_Garage = garage;
    		Drw = drw;
    
    		bindingSource.DataSource = \_Garage.Drawer\[Drw\];
    		
    	}
    
    private void UcDrawer\_Load(object sender, EventArgs e)
    	{
    		UpDnSettingsNumberOfTools.DataBindings.Add(new Binding("Value", bindingSource, "NumberOfTools", true, DataSourceUpdateMode.OnPropertyChanged));
    		
    	}
    

    }

    Form where usercontrol is used (it's loaded into tabpages in a tabcontrolon the form):

    public partial class FrmGarage : Form
    {
    private readonly int Drw;
    private readonly Garage _Garage;

    	public FrmGarage(int drw, Garage garage )
    	{
    		InitializeComponent();
    		Drw = drw;
    		\_Garage = garage;
    	}
    
    	private void FrmGarage\_Load(object sender, EventArgs e)
    	{
    		for (int i = 0; i < ch; i++)
    		{
    			AddTab(i + 1);
    		}
    	}
    
    	private void AddTab(int ChNum)
    	{
    		UcDrawer ucDrawer = new UcD
    
    L 1 Reply Last reply
    0
    • M Member_15011965

      I have a databinding issue with a numericupdown control bound to a bindinglist of objects. The updown control is located on a usercontrol. The controls can be loaded with values from a file located in a defaults xml file. The databinding works as it should upon loading the defaults file the first time. If during the session, I want to reload the defaults the databinding is lost. I can see that the values are loaded in correctly, but the numericupdn doesn't change value. here's a simplified version of my code to help explain. Object class:

      public class Garage :
      {

      public BindingList Drawer = new BindingList
      {
      new DrawerData(),
      new DrawerData()

      	};
      

      }

      public class DrawerData : INotifyPropertyChanged
      {
      private decimal _NumberOfTools;
      public event PropertyChangedEventHandler PropertyChanged;

      public decimal NumberOfTools
      	{
      		get { return \_NumberOfTools; }
      		set {
      			if (\_NumberOfTools != value)
      			{
      				\_NumberOfTools = value;
      
      				OnPropertyChanged("NumberOfTools");
      			}
      		}
      	}
      
      public event PropertyChangedEventHandler PropertyChanged;
      	public void OnPropertyChanged(\[CallerMemberName\] string PropertyName = "")
      	{
      		
      		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
      		
      	}
      

      }

      Usercontrol class:

      public partial class UcDrawer : UserControl, INotifyPropertyChanged
      {

      private readonly Garage \_Garage;
      private readonly int Drw
      BindingSource bindingSource = new BindingSource();
      
      public event PropertyChangedEventHandler PropertyChanged;
      
      public UcDrawer(int drw,Garage garage)
      	{
      		\_Garage = garage;
      		Drw = drw;
      
      		bindingSource.DataSource = \_Garage.Drawer\[Drw\];
      		
      	}
      
      private void UcDrawer\_Load(object sender, EventArgs e)
      	{
      		UpDnSettingsNumberOfTools.DataBindings.Add(new Binding("Value", bindingSource, "NumberOfTools", true, DataSourceUpdateMode.OnPropertyChanged));
      		
      	}
      

      }

      Form where usercontrol is used (it's loaded into tabpages in a tabcontrolon the form):

      public partial class FrmGarage : Form
      {
      private readonly int Drw;
      private readonly Garage _Garage;

      	public FrmGarage(int drw, Garage garage )
      	{
      		InitializeComponent();
      		Drw = drw;
      		\_Garage = garage;
      	}
      
      	private void FrmGarage\_Load(object sender, EventArgs e)
      	{
      		for (int i = 0; i < ch; i++)
      		{
      			AddTab(i + 1);
      		}
      	}
      
      	private void AddTab(int ChNum)
      	{
      		UcDrawer ucDrawer = new UcD
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Didn't see anything in there that looked like a "numeric up/down control".

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      M 1 Reply Last reply
      0
      • L Lost User

        Didn't see anything in there that looked like a "numeric up/down control".

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        M Offline
        M Offline
        Member_15011965
        wrote on last edited by
        #3

        It's in the UcDrawer class. The usercontrol layout was made in the design view. It is the UpDnSettingsNumberOfTools control located in this line:

        UpDnSettingsNumberOfTools.DataBindings.Add(new Binding("Value", bindingSource, "NumberOfTools", true, DataSourceUpdateMode.OnPropertyChanged));

        L 1 Reply Last reply
        0
        • M Member_15011965

          It's in the UcDrawer class. The usercontrol layout was made in the design view. It is the UpDnSettingsNumberOfTools control located in this line:

          UpDnSettingsNumberOfTools.DataBindings.Add(new Binding("Value", bindingSource, "NumberOfTools", true, DataSourceUpdateMode.OnPropertyChanged));

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Just a guess, since Windows Forms seem to be morphing into WPF ... Changing / selecting tabs fires its own loaded and unloaded events (in WPF) and it looks like your UpDown is on a tab.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups