0

I am making UWP app.I have a TextBox in xaml code in one class. I have given x:FieldModifier=public in its property so that it is accessible outside the class. I want to use in another class where it should get enable when toggle switch is on. So,I am making object of the class and using like this fnp.text.IsEnabled=true fnp is the object of class and text is name of textbox. but it is not firing. How do i do that?

    firstnavigatepage fnp = new firstnavigatepage();
    public peoplechatting()
    {
        this.InitializeComponent();
    }
public  void MyToggle_Toggled(object sender, RoutedEventArgs e)
    {

        ToggleSwitch toggleSwitch = sender as ToggleSwitch;
        if (toggleSwitch != null)
        {
            if (toggleSwitch.IsOn == true)
            {

                MyEllipse.Fill = new SolidColorBrush(Colors.Green);
                toggleSwitch.OnContent = "Bot";
                fnp.text.IsEnabled = true;
            }
  • It is not clear what is actually happening. What is not "firing"? – Martin Zikmund Jan 09 '18 at 11:08
  • is fnp the class or the instance? – BugFinder Jan 09 '18 at 11:09
  • there is a class firstnav it has a textbox by name "text" I have used property like IsEnabled=false.so,it is disabled currently .I want to use in another class that has toggle switch.I am making firstnav instance fnp then calling that textbox "text" .so inside toggle switch event handler i am writing fnp.text.IsEnabled=true.But in run time it does not enable ,it remains disabled.I dont know why it is not updating Isenable property – user9180315 Jan 09 '18 at 12:07

1 Answers1

0

Without any view of your code, it's pretty hard to tell.

But here is a few suggestion :

  • Verify your object is instantiated.
  • Verify your textbox is not currently used by another thread.

And here is some code that works on my computer :

MainWindow.cs

public partial class MainWindow : Window
{
    // My window object
    Screen win = new Screen();
    public MainWindow()
    {
        InitializeComponent();
        win.Show();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Simple toggle action
        if (win.myText.IsEnabled == true)
            win.myText.IsEnabled = false;
        else
            win.myText.IsEnabled = true;
    }
}

And here is the XAML of the "myText" textbox :

<TextBox x:Name="myText" x:FieldModifier="public"/>

Hope this helps.

EDIT : From your comments it would look like you are using an Async method to update the GUI.

You can't modify the GUI from another thread, unless you use some code to make it accessible. Check out this question for ways to make it accessible. (Check out Mark's answer, it's easy enough.)

Dastardly
  • 19
  • 10
  • there is a class firstnav it has a textbox by name "text" I have used property like IsEnabled=false.so,it is disabled currently .I want to use in another class that has toggle switch.I am making firstnav instance fnp then calling that textbox "text" .so inside toggle switch event handler i am writing fnp.text.IsEnabled=true.But in run time it does not enable ,it remains disabled.I dont know why it is not updating Isenable property – – user9180315 Jan 09 '18 at 12:13
  • firstnavigatepage fnp = new firstnavigatepage() public peoplechatting() { this.InitializeComponent(); public async void MyToggle_Toggled(object sender, RoutedEventArgs e) { ToggleSwitch toggleSwitch = sender as ToggleSwitch; if (toggleSwitch != null) { if (toggleSwitch.IsOn == true) MyEllipse.Fill = new SolidColorBrush(Colors.Green); toggleSwitch.OnContent = "Bot"; fnp.text.isenabled=true; – user9180315 Jan 09 '18 at 12:18
  • @user9180315 Updated my answer. – Dastardly Jan 09 '18 at 12:52
  • I tried to remove async and await it does not enable textbox.Even if i attach debugger.It does fier(becomes yellow) but not updating. – user9180315 Jan 09 '18 at 13:06
  • ,I have put image of my code.Please check that out – user9180315 Jan 09 '18 at 13:15
  • Avoid using image to showcase your code, Copy it in a Code block like I did in my post. – Dastardly Jan 09 '18 at 13:24