What's supposed to happen is the checkbox I have will flicker back and forth due to a timer. The variable changes, but the form does not. The name of the dataMember is Checked on both sides.
Class that's meant to be similar to implementation of INotifyPropertyChanged from here: Implementing INotifyPropertyChanged - does a better way exist?
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Notice
{
public class Notifier : INotifyPropertyChanged
{
//used to prevent infinite loops
private bool controllerChanged;
public event PropertyChangedEventHandler PropertyChanged;
public Notifier()
{
controllerChanged = false;
}
public bool SetField<T>(ref T field, T value, string propertyName)
{
if (!controllerChanged)
{
controllerChanged = true;
if (EqualityComparer<T>.Default.Equals(field, value))
{
controllerChanged = false;
return false;
}
field = value;
OnPropertyChanged(propertyName);
controllerChanged = false;
return true;
}
return false;
}
protected virtual void OnPropertyChanged(string property)
{
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property));
}
}
}
}
Class variable, just one for the example
using System;
using Notice;
namespace Vars
{
public class Checks
{
private bool checkbox;
Notifier note = new Notifier();
public Checks()
{
checkbox = true;
}
public bool Checkbox
{
get { return checkbox; }
//set { WindowsFormsApp1.Program.note.SetField(ref checkbox, value, "Checkbox"); }
set { note.SetField(ref checkbox, value, "Checkbox"); }
}
}
}
Most of the form code:
public Form1()
{
InitializeComponent();
//If you bind it using properties for the item in the form, don't need this line
checkBox1.DataBindings.Add("Checked", WindowsFormsApp1.Program.Ch, "Checkbox", true, DataSourceUpdateMode.OnPropertyChanged);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
WindowsFormsApp1.Program.Ch.Checkbox = !WindowsFormsApp1.Program.Ch.Checkbox;
Console.WriteLine(WindowsFormsApp1.Program.Ch.Checkbox);
}
private void timer1_Tick(object sender, EventArgs e)
{
WindowsFormsApp1.Program.Ch.Checkbox = !WindowsFormsApp1.Program.Ch.Checkbox;
Console.WriteLine(WindowsFormsApp1.Program.Ch.Checkbox);
}