简体   繁体   中英

Is it possible to change the Visibility property of a TextBox with the click of a button in another class(another wpf window)?

I'm trying to change the Visibility property of a TextBox in one window by clicking the button in another window. It does not work with the current code i have. It is easy to do with a button and a TextBox that are in the same class but in separate classes it doesn't work. This is the code behind for the window that has the button i want to press to change the other window's textbox.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Button2_Click(object sender, RoutedEventArgs e)
    {
        MainWindow mainWindow = new MainWindow();

        mainWindow.Box.Visibility = Visibility.Visible;
    }
}

}

this is the code for the window whose textbox i want to change

<Grid Background="Blue">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <TextBox Name="Box" Text="" Visibility="Hidden"/>
    <Button Name="Buttons" Grid.Column="1" Content="Button" Click="Button_Click"/>
</Grid>

 public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();

        Window1 window1 = new Window1();
        window1.Show();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Box.Visibility = Visibility.Visible;
    }
}

}

Yes it is! You can use Mediator class for that

public class Mediator
{
    static readonly Mediator instance = new Mediator();

    public static Mediator Instance
    {
        get
        {
            return instance;
        }
    }

    private Mediator()
    {

    }


    private static Dictionary<string, Action<object>> subscribers = new Dictionary<string, Action<object>>();


    public void Register(string message, Action<object> action)
    {
        subscribers.Add(message, action);
    }


    public void Notify(string message, Object param)
    {
        foreach (var item in subscribers)
        {

            if (item.Key.Equals(message))
            {
                Action<object> method = (Action<object>)item.Value;
                method.Invoke(param);
            }
        }
    }
}

Add this class to your project, then i constructor of your Main Window u create Instance of mediator

    Mediator _Mediator
    public MainWindow()
    {
        InitializeComponent();
        _Mediator= Mediator.Instance;
         _Mediator.Register("ButtonVisibility",ChangeButtonVisibility);

    }
    private void ChangeButtonVibility(object obj)
    {
      this.button.Visibility=Visibility.Hidden;
    }

in your other window constructor shoud look like this

    Mediator _Mediator;
    public MainWindow1(Mediator _mediator)
    {
        InitializeComponent();
        _Mediator=_mediator;

    }
    //ON button click u notify like this
     private void button_click(object sender, RoutedEventArgs e)
    {

        _Mediator.Notify("ButtonVisibility",null)
    }

When you initialize your child window from main window

MainWindow1 window1= new MainWindow(_Mediator);

NOTICE : Mediator class is used in MVVM patter , but can it can help in this case .

It is possible

By declaring the textbox as a static variable ( in the code not in the xaml file )you can delacre it in the xaml file but you need to use

X:Name="choose_the_name_you_want"

And then reach it from the cs file

And just do it like this

I want the x:name to be exmpl so my code will be

Public Static textblock TB =new exmpl ;

Then just edit TB.Visibility to false

That's it

Declare the static variable out of any void Nut inside the class

Don't forget to comment down the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM