Answer the question
In order to leave comments, you need to log in
How to change checkbox value from another window?
How can I change the value of a checkbox from another window? there are 2 windows, in one checkbox with a method in the code where the checkbox value can change, and in the other window a method that causes the method to fire in order to change the value. I do not understand how you can call a method from another window?
Answer the question
In order to leave comments, you need to log in
As always, there is a simple and correct way to solve the problem. The
simple method is to simply call the method of this window. You display it somewhere, right? Get a field with the type of your window with the method, where you show the window - save it there. Change the checkbox - access the field, call the method. Something like this (the code did not run, somewhere it may be necessary to write a little differently):
public class MainWindow : Window
{
private WindowWithMetod _windowWithMetod;
// ........................................................................................
private void DisplaySecondWindow()
{
_windowWithMethod = new WindowWithMethod;
_windowWithMethod.Closed += (s, e) => _windowWithMethod = null;
_windowWithMethod.Show();
}
private void HandleCheckboxValueChanged(object sender, EventArgs args)
{
_windowWithMethod?.Method();
}
}
public interface IBooleanState
{
public bool State { get; set; }
}
public interface IBooleanStateEvents
{
public event EventArgs<bool> StateChanged;
}
public sealed class EventArgs<T> : EventArgs
{
public EventArgs(T value)
{
Value = value;
}
public T Value { get; }
}
public sealed class BooleanState : IBooleanState, IBooleanStateEvents
{
private bool _state;
public event EventArgs<bool> StateChanged;
public bool State
{
get => _state;
set
{
if (value == _state)
{
return;
}
_state = value;
StateChanged?.Invoke(this, new EventArgs(value));
}
}
}
public class MainWindow : Window
{
private readonly IBooleanState _state;
// ..................
public MainWindow(IBooleanState state)
{
_state = state;
}
}
public class WindowWithMethod : Window
{
private readonly _stateEvents;
public WindowWithMethod(IBooleanStateEvents stateEvents)
{
_stateEvents = stateEvents;
}
}
// Наверное где-то в Program.cs
private readonly BooleanState _state = new BooleanState();
//................................................
Application.Run(new MainWindow(_state));
//................................................
new WindowWithMethod((IBooleanStateEvents)_state);
public class MainWindow : Window
{
private void HandleCheckboxValueChanged(object sender, EventArgs args)
{
_state.State = _checkBox.Checked;
}
}
public class WindowWithMethod : Window
{
// .................
public WindowWithMethod(IBooleanStateEvents stateEvents)
{
// ........................
_stateEvents = stateEvents;
_stateEvents.StateChanged += HandleStateChanged;
// .........................
}
private void HandleStateChanged(object sender, EventArgs<bool> args)
{
Method(args.State);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question