Winforms Usercontrol Databinding question
-
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
-
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
-
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
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));
-
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));
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