Search code examples
c#checkboxwpf-controlstextblock

How do i change the status of check box and to change text block's value of already opened parent window


Im developing an application in C# under WPF.I want to change the status of the check box and also i need to change the text block's value of already opened window from the currently working windows operation and to update that opened window with these changes(Refresh the already opened window with some updates).


Solution

  • In order to control UI elements from code behind you need to assign a name to each UI element you would like to control.

    As for check box decleared as

    <CheckBox Name="chkA"> Checkbox A </CheckBox>
    

    you can change its' checked state from code-behind via

    chkA.IsChecked = true; 
    

    As for the diffenet window update - your Windows in WPF are just classes, part of which usually lives in *.xaml file, and another in the corresponding *.cs file.

    If you declare a public method that refreshed the windows content's as you want in your second windows' class, and, when you will be creating your second window, you somehow save the reference to its' instance available in a first class (or some other logic in your application), you will be able to simply call that method from windows' 1 code to refresh the second widows appearance, as declared in a method called.

    Basically, from Windows1 you call:

    MySecondWindow secW = new MySecondWindow(); 
    
    secW.Show();
    
    ....
    
    secW.RefreshWithMyChages();
    

    RefreshWithMyChages() is just a public method in your second windows' class codebehind.

    All of this hold true if:

    • both of your windows are in the same project
    • you are not willing to use MVVM or other UI-patterns.