rog_rickert
Member
- Joined
- Dec 17, 2020
- Messages
- 11
- Programming Experience
- Beginner
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<DrawerData> Drawer = new BindingList<DrawerData>
{
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 UcDrawer(Drw, _Garage) { Dock = DockStyle.Fill };
TabPage myTabPage = new TabPage();
myTabPage.Controls.Add(ucDrawer);
GarageTabControl.TabPages.Add(myTabPage);
myTabPage.Name = "Drawer" + Drw;
myTabPage.Text = "Drawer " + Drw;
}
}
}
Main form (has buttons on a side list that load different forms into a panel, i'll only include the garage one for simplicity):
public partial class Form1 : Form
{
public Garage garage;
readonly private FrmGarage FrmGarage_vrb;
private Form ActiveMainForm = null;
public Form1()
{
InitializeComponent();
garage = new Garage();
FrmGarage_vrb = new FrmGarage(2, garage) { Dock = DockStyle.Fill, TopLevel = false, TopMost = true };
}
//Form loader code when one of the side buttons is clicked
private void MainButtonListClickAction(Button buttonClicked, Form formActivated)
{
pnlNav.Height = buttonClicked.Height;
pnlNav.Top = buttonClicked.Top;
this.pnlFormLoader.Controls.Clear();
this.pnlFormLoader.Controls.Add(formActivated);
formActivated.Show();
ActiveMainForm = formActivated;
}
public void BtnOverride_Click(object sender, EventArgs e)
{
MainButtonListClickAction(BtnGarage, FrmGarage);
}
//When the New button is pressed, a defaults file is loaded
private void BtnMainNew_Click(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(BindingList<DrawerData>));
XmlReader reader = XmlReader.Create("defaults.xml");
garage.Drawer = (BindingList<DrawerData>)serializer.Deserialize(reader);
reader.Close();
}