XAML part:

<TextBlock x:Name="myGroup" IsEnabled="{Binding Enable}"></TextBlock> 

I describe the property, the class above is inherited from the INotifyPropertyChanged interface (public partial class Index: Window, INotifyPropertyChanged):

 public event PropertyChangedEventHandler PropertyChanged; public bool _enable; public bool Enable { get { return _enable; } set { _enable = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(Enable))); } } 

I create a method to check if there is an entry in the database, based on the result of the check, the value of IsEnabled should change:

 public void CheckEnable(bool result) { string connectionString = "SERVER = localhost; DATABASE = vbnzt; UID = vbnz; PASSWORD = ;"; MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand check_group = new MySqlCommand("SELECT * FROM groups WHERE liderId = " + Properties.Settings.Default.id.ToString(), connection); connection.Open(); MySqlDataReader cg_result = check_group.ExecuteReader(); if (cg_result.HasRows) { result = true; } else { result = false; } _enable = result; connection.Close(); } 

I call the check before loading the content (in the MainWindow_Loaded method, which is bound to this.Loaded + = MainWindow_Loaded; in the constructor):

 CheckEnable(_enable); 

In another class (clicking the button), I call the check, passing true to it:

 Index index = new Index(); index.CheckEnable(true); 

It is necessary that when you click on this button without restarting the application, the TextBlock value IsEnabled changes.

  • one
    If you change the value of an anchored property from the code and you need the UI to react to it, then in the setter of this property you need to trigger the PropertyChanged event. - Bulson
  • Already tried and through INotifyPropertyChanged, nothing leaves. I think I'm complicating, it should be made easier. I will try to explain in words, maybe tell me something. - Dmitriy
  • There is an Index class, in it, by and large, there are 3 buttons that call up 3 different frames, I need to do this so that until the check from one of the frames passes (the check is described above in the code), one of these three the buttons (which are described not in the frame, but in the Index class) were IsEnabled = false. The check is now working, but the IsEnabled changes are applied only after the application is restarted. - Dmitriy
  • one
    PropertyChangedEventArgs(nameof(CreateGroup.createGroup))); it is impossible to do so, you must refer to a property belonging to the same class, and not to another. Think for yourself: the first argument this tells which class the change occurred in, and the second argument gives the name of the property from the same class. - Bulson
  • one
    @Dimitri: Ah, this is the Window! Okay, it's warmer. How do you install the DataContext? Wangju, that in any way. - VladD

1 answer 1

In general, if you have already started working with bandages, do it using the mvvm pattern, at first it will be difficult, the code will become larger, but from my personal experience I will say that it will be much easier to manage the data. And yes, in the property you raise the event propertychanged? In xaml

  <TextBlock x:Name="myGroup" IsEnabled="{Binding Enable, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">. 

UpdateSourceTrigger = PropertyChanged - listens for an event

  public void NotifyPropertyChanged([CallerMemberName] string property = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); } 

This method should be called on the Set properties.