user3008134
user3008134

Reputation: 127

How to create textbox dynamically?

I just want to create a textbox, which appears only if certain conditions are met! What i want is that, if the conditions are met the textbox should appear.

If the conditions are not met, then it should not be visible (not just changing the visibility of the textbox to collapsed),i.e there should not be an empty place too.

How can i do this??

Upvotes: 0

Views: 251

Answers (4)

Jaihind
Jaihind

Reputation: 2778

I think there is no need to create TextBox dynamically. you should manage it by creating TextBox in xaml and TextBox Visibility. When you create our TextBox in xaml remember that Height should be default of TextBox. Here is the sample

<TextBox x:Name="txtVisible" Visibility="Collapsed"/>

 if(Condition==met)
   txtVisible.Visibility=Visibility.Visible;
 else
  txtVisible.Visibility=Visibility.Collapsed;

Upvotes: 1

A.K.
A.K.

Reputation: 3331

Add a textbox inside the grid object like

    Grid grid=new Grid(){Height=60,Width=100};
    Textbox tBox=new Textbox(){Text="Sample",Visibility=Visibility.Collapsed};
    grid.Children.Add(tBox);

now conditions

if(Condition==true)
{
tBox.Visibility=Visibility.Visible;
}
else
{
tBox.Visibility=Visibility.Collapsed;
}

Upvotes: 1

user3157205
user3157205

Reputation: 1

You can use panels I think. Put something under to panel, put panel and then put textbox on it. And make panel invisible by default. If conditions are met u can make panel visible by code

Upvotes: -1

Illia Tereshchuk
Illia Tereshchuk

Reputation: 1202

XAML provides rich data binding mechanism for MVVM pattern. You have to:

  1. Implement INotifyPropertyChanged in ViewModel
  2. Create a property with PropertyChanged event of INotifyPropertyChanged fired in setter in ViewModel with data type of TextBox visibility
  3. In XAML, bind this property to your TextBox

Then, changing this property somewhere automatically leads to changing TextBox state

Upvotes: 1

Related Questions