Skip to main content

Microsoft Silverlight

Answered Question TextBox not working after being hiddenRSS Feed

(0)

jesusgarza
jesusgarza

Member

Member

2 points

5 Posts

TextBox not working after being hidden

I have a form to search for text. When the user presses the "search" button I disable it to prevent him from attempting another search while the program is calling the webservice that resolves the search.
Once I returned from the call, I can't capture anymore text on the textbox.
I am including the code from three files: Page.xaml, Page.cs and SearchText.cs. Instead of a webservice I use a "cancel" button, but the result is the same.
If I remove one of the Visibility or IsEnabled properties, the textbox works!!!
There is something similar on:
http://silverlight.net/forums/p/57420/148128.aspx#148128

In Page.xaml:
        <StackPanel Orientation="Vertical" Grid.Row="0">
            <TextBox x:Name="txtToSearch"
                    Text="{Binding Path=TextToSearch, Mode=TwoWay}"
                    Visibility="{Binding Path=SearchVisibility}"                
                     />
            <Button x:Name="btnStartSearch" Content="Search" Grid.Row="1"
                    Visibility="{Binding Path=SearchVisibility}"                
                    IsEnabled="{Binding TextToSearchVisible}"
                    />
            <Button x:Name="btnCancelSearch" Content="Cancel" Grid.Row="2"
                    Visibility="{Binding Path=CancelVisibility}"                
                    />
        </StackPanel>

In Page.cs:
    public partial class Page : UserControl
    {
        SearchText searchText;
        public Page()
        {
            InitializeComponent();

            searchText = new SearchText();
            this.DataContext = searchText;

            this.btnStartSearch.Click += new RoutedEventHandler(btnStartSearch_Click);
            this.btnCancelSearch.Click += new RoutedEventHandler(btnCancelSearch_Click);
        }

        void btnStartSearch_Click(object sender, RoutedEventArgs e)
        {
            searchText.TextToSearchVisible = false;
        }

        void btnCancelSearch_Click(object sender, RoutedEventArgs e)
        {
            searchText.TextToSearchVisible = true;
        }
    }

SearchText class:

using System.ComponentModel;

    public class SearchText : INotifyPropertyChanged
    {
        private string _textToSearch;
        public string TextToSearch
        {
            get { return this._textToSearch; }
            set
            {
                this._textToSearch = value;
                this.RaisePropertyChanged("TextToSearch");
            }
        }

        private bool _textToSearchVisible = true;
        public bool TextToSearchVisible
        {
            get { return this._textToSearchVisible; }
            set
            {
                this._textToSearchVisible = value;
                this.RaisePropertyChanged("TextToSearchVisible");

                SearchVisibility = _textToSearchVisible ? Visibility.Visible : Visibility.Collapsed;
                CancelVisibility = _textToSearchVisible ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        private Visibility _searchVisibility = Visibility.Visible;
        public Visibility SearchVisibility
        {
            get { return this._searchVisibility; }
            set
            {
                this._searchVisibility = value;
                this.RaisePropertyChanged("SearchVisibility");
            }
        }

        private Visibility _cancelVisibility = Visibility.Collapsed;
        public Visibility CancelVisibility
        {
            get { return this._cancelVisibility; }
            set
            {
                this._cancelVisibility = value;
                this.RaisePropertyChanged("CancelVisibility");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

    }

fullsailrick
fullsail...

Contributor

Contributor

3699 points

829 Posts

Re: TextBox not working after being hidden

Hi! You should use the IsReadOnly property (set it to true while your process is working in the background), or you can do the same thing by setting the IsHitTestVisible to false, and changing the Opacity value accordingly. :)

jesusgarza
jesusgarza

Member

Member

2 points

5 Posts

Re: TextBox not working after being hidden

 Fullsailrick, thanks for your answer.

In the button I replaced IsEnabled for IsHitTestVisible. Now at the end of a second cycle the textbox doesn't work (search - cancel - search - cancel - textbox not working).

Is it a bug or it is my program?

fullsailrick
fullsail...

Contributor

Contributor

3699 points

829 Posts

Re: TextBox not working after being hidden

It's probably not working because you have to remember to set IsHitTestVisible back to true after your work is completed in the background.

jesusgarza
jesusgarza

Member

Member

2 points

5 Posts

Re: TextBox not working after being hidden

Thanks for sticking by fullsailrick. I already removed the IsHitTestVisible line from the XAML and its still failing exactly as I described. Remember, my problem is that the textbox doesn't accept text.

 

jesusgarza
jesusgarza

Member

Member

2 points

5 Posts

Answered Question

Re: TextBox not working after being hidden

If I put a <ContentControl> around the text box it works normally.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities