A set of .NET Framework managed libraries for developing graphical user interfaces.
Hi,@Reza Jaferi . Welcome to Microsoft Q&A Forum. You could use INotifyPorpertyChanged is a good approach for achieving this.
This interface allows you to notify subscribers (like UI elements) when a property changes, and in your case, you want to trigger some action when the Background property changes.
Here's what has changed:
1.Implemented INotifyPropertyChanged.
2.Modified the properties to call OnPropertyChanged when their values change.
3.Removed the parameters from SetBackgroundColor because it can directly access the properties.
public class UserInterfaceInitializer : INotifyPropertyChanged
{
public enum Color
{
red,
green,
blue,
white
}
private Color? _background;
private Control _myControl;
public Color? Background
{
get { return _background; }
set
{
if (_background != value)
{
_background = value;
OnPropertyChanged(nameof(Background));
SetBackgroundColor();
}
}
}
public Control MyControl
{
get { return _myControl; }
set
{
if (_myControl != value)
{
_myControl = value;
OnPropertyChanged(nameof(MyControl));
SetBackgroundColor();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SetBackgroundColor()
{
// Use the properties directly, no need to pass them as parameters
Color color = Background ?? Color.white;
Control myControl = MyControl;
// Changing the background color of that control automatically with a specific operation is done here.
// You can access the properties directly.
}
}
In your Window_Loaded method, you can set the properties like before, and the corresponding actions will be triggered:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var customUser = new UserInterfaceInitializer();
customUser.MyControl = TotalTextBox;
customUser.Background = UserInterfaceInitializer.Color.green;
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.