I'm having a problem when inserting a UserControl in a form. I drag my UserControl onto the form, save, and get the following screen:
I searched for SO and there is someone who had the same problem as me but I can't understand the answer or what to say with it.
I briefly show you the code and some indications so that you can guide me on the matter.
This is the UserControl and how it is bound:
The class that is bound to bsControl is the following:
namespace SmartInventorySystem.ViewModel.Controls
{
public class DispenseCartControlViewModel : FormViewModel
{
public DispenseCartControlViewModel()
{
ItemsToCheckout = new List<ItemToCheckoutRowViewModel>();
}
public List<ItemToCheckoutRowViewModel> ItemsToCheckout { get; set; }
public decimal SubTotal
{
get; //=> ItemsToCheckout.Sum(x => x.Quantity * x.UnitPrice);
set;
}
public decimal Discount { get; set; }
public decimal Vat { get; set; }
public decimal Total
{
get; //=> SubTotal - Discount + Vat > 0 ? (Vat / 100 * (SubTotal - Discount)) : 0;
set;
}
}
}
And finally, the code that has the control:
public partial class DispenseCartControl : UserControl, IDispenseCartView
{
private readonly DispenseCartPresenter _presenter;
public DispenseCartControl()
{
InitializeComponent();
_presenter = new DispenseCartPresenter(this);
State = new DispenseCartControlViewModel();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DispenseCartControlViewModel State { get; set; }
public void ShowError(Exception ex)
{
MessageBox.Show(this, ex.Message, "Dispense Cart", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public void ShowInfo(string msg)
{
MessageBox.Show(this, msg, "Dispense Cart", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public void UpdateFormBindingSource()
{
bsControl.ResetBindings(false);
}
private void DispenseCartControl_Load(object sender, EventArgs e)
{
bsControl.DataSource = State;
}
}
Update 1 : I add the call-stack where the designer throws me an error.
at System.Windows.Forms.BindToObject.CheckBinding() at System.Windows.Forms.BindToObject.SetBindingManagerBase(BindingManagerBase lManager) at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) at System.Windows.Forms.ListManagerBindingsCollection.AddCore (Binding dataBinding) at System.Windows.Forms.BindingsCollection.Add(Binding binding) at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding) at System.Windows.Forms.Control.UpdateBindings() at System. Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnParentBindingContextChanged EventArgs e) at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value) at System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext value) at System.Windows.Forms.ContainerControl. get_BindingContext() at System.Windows.Forms.Control.get_BindingContextInternal() at System.Windows.Forms.SplitContainer.get_BindingContext() at System.Windows.Forms.Control.get_BindingContextInternal() at System.Windows.Forms.Control.get_BindingContext( ) at System.Windows.Forms.Control.get_BindingContextInternal() at System.Windows.Forms.ContainerControl.get_BindingContext() at System.Windows.Forms.Control.get_BindingContextInternal() at System.Windows.Forms.ContainerControl.get_BindingContext() at System.Windows.Forms.Control.UpdateBindings( ) at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) at System.Windows.Forms.ContainerControl.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control. CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows. Forms.Control.ControlCollection.Add(Control value) at System.Windows.Forms.Form.ControlCollection.Add(Control value) at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
I quote myself to tell about this experience.
Doing some more digging, I found a great article for those of you who have problems with design-time bugs: Walkthrough: Debugging Custom Windows Forms Controls at Design Time
I started debugging the VS designer, according to the guide, and I notice that there is no problem in the initialization of the control, nor in the binding. Frustrated, I try several variants but nothing.
So, I closed everything... Magically, when I open VS again and the solution, the control is shown in the designer without any problem.
Moral of the story: " Turn off and on again " solutions fix many problems. More than anything because this solution talks about (re-initializing) the states (of variables and cache) of the application.
Every time this fix is applied, all states are reset and the cache is clean. In this case, it was VS. I speculate that it was that, in the designer's cache, it had an old version of the control and/or the form where the control was.