The CheckedChanged event of a CheckBox is fired when the page is loading up (on data binding), in addition to the user actually checking/unchecking.
Is there a way to detect whether CheckedChanged event was fired by a user action or otherwise?
In WinForms/WPF, I used to define a boolean variable like Loading = true; and then used to set Loading = false; once page load is finished. Then, the CheckedChanged event would check if Loading is false to execute the logic. Here's how I used to do it:
private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
if (Loading == true) return;
//execute logic...
}
With async functions all over, I am unable to fit the Loading trick correctly. Is there a better way?
In WinForms/WPF, I used to define a boolean variable like Loading = true; and then used to set Loading = false; once page load is finished. Then, the CheckedChanged event would check if Loading is false to execute the logic. Here's how I used to do it:
I change bool userchecked value before and after setting BindingContext = new checkmodel(), to To detect whether the CheckBox_CheckedChanged first time trigger is user or data binding.
public partial class Page18 : ContentPage
{
private bool userchecked;
public Page18()
{
InitializeComponent();
userchecked = false;
this.BindingContext = new checkmodel();
userchecked = true;
}
private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
if(userchecked==true)
{
Console.WriteLine("user checked!");
}
else
{
Console.WriteLine("don't user checked!");
}
}
}
public class checkmodel:ViewModelBase
{
private bool _ischecked;
public bool ischecked
{
get { return _ischecked; }
set
{
_ischecked = value;
RaisePropertyChanged("ischecked");
}
}
public checkmodel()
{
ischecked = true;
}
}
The ViewModelBase is class that implementing INotifyPropertyChanged, to notify data update.
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Related
I am using MVVM light in WPF. The Model class properties are constantly changed due to changes of the underlying data-access layer.
Model:
public class SBE_V1_Model : ViewModelBase
{
public SBE_V1_Model(String name)
{
Name = "MAIN." + name;
SetupClient();
}
private void SetupClient()
{
client = new ConnectionHelper(this);
client.Connect(Name);
}
public Boolean Output
{
get
{
return _Output;
}
set
{
if (value != this._Output)
{
Boolean oldValue = _Output;
_Output = value;
RaisePropertyChanged("Output", oldValue, value, true);
}
}
}
}
If Output property changes, then bindings will be notified, so this works. But what is the correct way to update the property from the data-access source, which knows the new value?
public class ConnectionHelper : ViewModelBase
{
public Boolean Connect(String name)
{
Name = name;
tcClient = new TcAdsClient();
try
{
dataStream = new AdsStream(4);
binReader = new AdsBinaryReader(dataStream);
tcClient.Connect(851);
SetupADSNotifications();
return true;
}
catch (Exception ee)
{
return false;
}
}
private void tcClient_OnNotification(object sender, AdsNotificationEventArgs e)
{
String prop;
notifications.TryGetValue(e.NotificationHandle, out prop);
switch (prop)
{
case "Output":
Boolean b = binReader.ReadBoolean();
RaisePropertyChanged("Output", false,b, true);
break;
}
}
}
Why doesnt the RaisePropertyChanged call in connectionhelper update the property of the model? If this is the wrong way, should I set up some kind of listener?
In your SBE_V1_Model class you should subscribe to receive PropertyChange notifications from the ConnectionHelper ViewModel.
// Attach EventHandler
ConnectionHelper.PropertyChanged += OnConnectionHelperPropertyChanged;
...
// When property gets changed, raise the PropertyChanged
// event of the ViewModel copy of the property
OnConnectionHelperPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Something") //your ConnectionHelper property name
RaisePropertyChanged("Ouput");
}
Also look into MVVM light messenger. Here is a link you might be interested from StackOverflow.
You should only use PropertyChanged in the ViewModel, not in the Model.
You can use PropertyChanged in Models only in special times.
RaisePropertyChanged("Output", false,b, true);
In that PropertyChanged you are always saying that Output Property was changed.
I recommend you implement INotifyPropertyChanged
class MyClass : INotifyPropertyChanged
{
public bool MyProperty{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
To notify any property change you have to use:
OnPropertyChanged("MyProperty");
I'm having a hard time binding properties. I'm probably missing something here. The thing is that my object properties are not updated by the interface control changes until I do another interaction with the UI, like press an empty dummy button. This is really odd. What I'm missing here? How can I make the bind property to be updated on control checked change?
Sample:
Create an empty Form, add a CheckBox and a button.
Bind the Checkbox to the IsOnSale Car property.
Add a console writeline to see when IsOnSale property will be changed.
Build and click on the Checkbox, IsOnSale wont change (no console msg) until a click on another button or something.
Click on the dummy button, the IsOnSale property will be changed! What!??
private Car car;
public Form1()
{
InitializeComponent();
car = new Car();
car.IsOnSale = false;
checkBox1.DataBindings.Add("Checked", car, "IsOnSale");
}
//U dont need this for the test..
private void btnPrintStatus_Click(object sender, EventArgs e)
{
string s = car.ToString();
Console.WriteLine(s);
}
Car is just a class that implements INotifyPropertyChanged. Also, It prints on console every time a property change request is made (just for debugging). As you can see if you build the sample, the change request is only made after a click on the dummy button...
internal class Car : INotifyPropertyChanged, INotifyPropertyChanging
{
private string name;
public string Name
{
get { return name; }
set
{
string propName = GetName(new { Name });
UpdateField(ref name, value, propName, null);
}
}
private bool isOnSale;
public bool IsOnSale
{
get { return isOnSale; }
set
{
string propName = GetName(new { IsOnSale });
UpdateField(ref isOnSale, value, propName, null);
}
}
public override string ToString()
{
return string.Format("Car Name: {0}. IsOnSale: {1}", Name, IsOnSale);
}
#region INotifyPropertyChanged, INotifyPropertyChanging
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
public static string GetName<T>(T item) where T : class
{
return typeof(T).GetProperties()[0].Name;
}
protected bool UpdateField<T>(ref T field, T newValue, string propertyName, Action action = null)
{
bool needChange = !EqualityComparer<T>.Default.Equals(field, newValue);
Console.WriteLine("Try to UpdateField {0}. Need update? {1}", propertyName, needChange);
if (needChange)
{
OnPropertyChanging(propertyName);
field = newValue;
if (action != null)
action();
OnPropertyChanged(propertyName);
}
return needChange;
}
protected void OnPropertyChanging(string propertyName)
{
PropertyChangingEventHandler handler = PropertyChanging;
if (handler != null)
handler(this, new PropertyChangingEventArgs(propertyName));
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
EDIT - workaround
I have been able to find a work around, but it smells..
I'm using a BackgroundWorker to "get out" of the change event and after that I perform a click on a dummy button.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//Wont work, need to get out of this event.
//btnDummy.PerformClick();
//Using another thread to get out of this event:
RunAsyncBindFixer();
}
private void RunAsyncBindFixer()
{
var workerBindFixer = new BackgroundWorker();
workerBindFixer.DoWork += WorkerBindFixer_DoWork;
workerBindFixer.RunWorkerCompleted += WorkerBindFixer_RunWorkerCompleted;
Console.WriteLine("Starting RunAsyncBindFixer");
workerBindFixer.RunWorkerAsync();
}
void WorkerBindFixer_DoWork(object sender, DoWorkEventArgs e)
{
//Aparently we need nothing here.
//Thread.Sleep(0);
}
void WorkerBindFixer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("RunAsyncBindFixer RunWorkerCompleted");
btnDummy.PerformClick();
//Checked change on a dummy checkbox wont work also
//ckbDummy.Checked = !ckbDummy.Checked;
Console.WriteLine("Dummy action applied");
}
Databinding defaults to writing the value whenever the bound control is validated, which happens normally when the control loses focus (such as when a button is pressed as in your example)
You can force the databinding to use the onpropertychanged of the control provided the control supports it (in this case CheckedChanged , which is supported by the checkbox control)
checkBox1.DataBindings.Add("Checked", car, "IsOnSale", true, DataSourceUpdateMode.OnPropertyChanged);
I have a form, I select some checkboxes, edit some text field, select from a combobox etc. then I click Exit. Based on the fact that "Data has been changed ??" I wish to perform actions. The problem is I can't get the event work :
private void DataChanged(object sender, EventArgs e)
{
MessageBox.Show("Data is changed", "debug");
isDataSaved = false;
}
When is this method called, how do I make it work? Is this supposed to get fired when the form's fields have some data i.e filL a text box ?
I dont really get the API: DataChanged event
Note: I'm following Mike Murach C# 5th edition chapter 10 example.
Edit (exact words from book):
Generate an event handler named DataChanged for the
SelectedIndexChanged event of the XXXX Name combo box. Then , wire
this event handler to the TextChanged event of the YYYYY Method label
and add the code to this event handler so it sets the isDataSaved
variable to false
When I double click on the commbo box the generated event handler it is not named DataChanged but cboNames_SelectedIndexChanged... (is this a book screw up or me total noob ? PS: There is no .. 'database' in the project)
Personally I mostly use databinding these days to get notified of changes in data.
A data holder class, which implements INotifyPropertyChanged. This interface gives you the possibility to get notified when the value of a property changes.
public class SomeData: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "") {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
}
private boolean _someBoolean;
public int SomeBoolean {
get { return _someBoolean; }
set {
SetProperty(ref _someBoolean, value);
}
}
private string _someString;
public string SomeString {
get { return _someString; }
set {
SetProperty(ref _someString, value);
}
}
// etc
}
Now our form, which uses the data class and it's INotifyPropertyChanged implementation to get notified when a change in data occurs.
public partial class SomeForm: Form {
private SomeData _data;
private void LoadData() {
_data = new SomeData();
_data.PropertyChanged += Data_PropertyChanged;
}
private void SaveData() {
// TODO: Save data
}
private void AddDataBindings() {
checkbox1.DataBindings.Add("Checked", _data, "SomeBoolean");
textbox1.DataBindings.Add("Text", _data, "SomeString");
// add other
}
private void Data_PropertyChanged(object sender, PropertyChangedEventArgs e) {
// Here you can add actions that must be triggered when some data changes.
if (e.PropertyName == "SomeBoolean") {
// Do something when some-boolean property changes
}
// Set is-changed-boolean to true to 'remember' that something has changed.
_isChanged = true;
// Give message
MessageBox.Show(string.Format("Data changed, property {0}", e.PropertyName));
}
private bool _isChanged = false;
protected void Form_Closed(object sender, EventArgs e) {
// If data is changed, save it
if (_isChanged) {
SaveData();
}
}
}
Your problem is not known where the method DataChanged use and how. I have a suggestion for you that is use Focus Activated in properties.Add datachanged printing method Activated
good luck.
You must make properties this like
I have a property for userControl like this:
public enum Mode { Full, Simple }
public Mode NavigatorMode { get; set; }
and now, I need to write an event, when user change the property (NavigatorMode) form Full mode to simple mode, or reverse
how can I do that?
Implement INotifyPropertyChanged to your class:
public class YourClass : INotifyPropertyChanged
{
// Your private variable
private Mode mode;
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public YourClass()
{
}
public Mode NavigatorMode
{
get { return mode; }
set
{
mode = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged(mode);
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(Mode modeParam)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(modeParam));
}
}
}
How about implementing the INotifyPropertyChanged interface in your control?
Or simply writing a custom event:
public event EventHandler<Mode> ModeChanged;
public Mode NavigatorMode
{
get { return _navigatorMode; }
set
{
_navigatorMode = value;
if(ModeChanged != null)
ModeChanged(this, _navigatorMode);
}
}
And outside your usercontrol you can handle that event and do something based on the mode.
For an application I have to use a custom button that react when one of its properties value is being changed. I addded a field named Data to the new button:
public class ButtonData
{
public string Name;
public string Color;
//And more stuff...
}
Then I have the folowing code for new button, I want it to be able to update itself (change background color and some other stuff) whenever its Data property gets updated from somewhere in application. I found some ideas about implementing INotifyPropertyChanged interface and I set it up in my custom button like this:
public partial class ButtonPin : Button, INotifyPropertyChanged
{
private ButtonData _data;
public ButtonData Data
{
get { return _data; }
set
{
if (value == _data) return;
_data = value;
OnPropertyChanged("Data");
}
}
private bool _buttonDataAdded;
public ButtonPin()
{
InitializeComponent();
}
public ButtonPin(ButtonData data)
{
Data = data;
_buttonDataAdded = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Now I am not sure how to use this! For example, if the Color in Data objects gets changed
somehow somewhere and its get assigned to current button's Data field, I want this button to change its background color. Something like
var data = new ButtonData();
data.Name = "Hi!";
data.Color = Color.Red;
buttonPin1.Data = data; //Here I need the changes to occur
You have implemented the interface INotifyPropertyChanged on the ButtonPin class, not on the ButtonData class, and you want to detect a change on an object of type ButtonData, thus you need to implement the interface INotifyPropertyChanged on the ButtonData class.
To detect the change you need to hook up to the PropertyChanged event of the ButtonData object in the setter of the ButtonPin.Data property. Something like this.
private bool _data;
public ButtonData Data {
get { return _data; }
set {
if (value != _data) {
// Unhook previous events
if (_data != null)
_data.PropertyChanged -= HandleButtonDataPropertyChanged;
// Set private field
_data = value;
// Hook new events
if (_data != null)
_data.PropertyChanged += HandleButtonDataPropertyChanged;
// Immediate update since we have a new ButtonData object
if (_data != null)
Update();
}
}
}
private void HandleButtonDataPropertyChanged(object sender, PropertyChangedEventArgs e) {
// Handle change in ButtonData
Update();
}
private void Update() {
// Update...
}