I've tested some thinks with Dynamic Textboxes and Buttons, Adding Items to my Grid works very well, but if I want to Delete it there are some Bugs, sometimes 1 Row is empty and my Add Button disappears or my Program crashs.
What did I wrong or what did I missed?
C# Code:
public MainWindow()
{
InitializeComponent();
}
int Numberic = 0;
private void NewButton_Click(object sender, RoutedEventArgs e)
{
#region Row and Numberic
Numberic++;
RowDefinition ROW = new RowDefinition();
GridLength Height = new GridLength(59);
ROW.Height = Height;
MainGrid.RowDefinitions.Add(ROW);
#endregion
#region Set new Button Location
int ButtonLocation = Grid.GetRow(NewButton);
Grid.SetRow(NewButton, ButtonLocation + 1);
#endregion
#region Create TextBox
TextBox CreateTextBox = new TextBox();
CreateTextBox.Name = "NewTextBox_" + Numberic;
CreateTextBox.Width = 438;
CreateTextBox.Height = 35;
CreateTextBox.Margin = new Thickness(53, 12, 0, 0);
CreateTextBox.HorizontalAlignment = HorizontalAlignment.Left;
CreateTextBox.VerticalAlignment = VerticalAlignment.Top;
CreateTextBox.FontSize = 15;
CreateTextBox.HorizontalContentAlignment = HorizontalAlignment.Left;
CreateTextBox.VerticalContentAlignment = VerticalAlignment.Center;
MainGrid.Children.Add(CreateTextBox);
Grid.SetRow(CreateTextBox ,ButtonLocation);
#endregion
#region Create Button
Button CreateButton = new Button();
CreateButton.Name = "NewButton_" + Numberic;
CreateButton.Width = 35;
CreateButton.Height = 35;
CreateButton.Margin = new Thickness(12, 12, 0, 0);
CreateButton.HorizontalAlignment = HorizontalAlignment.Left;
CreateButton.VerticalAlignment = VerticalAlignment.Top;
CreateButton.Content = "-";
CreateButton.FontSize = 20;
CreateButton.FontWeight = FontWeights.Bold;
BrushConverter BC = new BrushConverter();
CreateButton.Background = (Brush)BC.ConvertFrom("#FFDB0000");
CreateButton.Foreground = Brushes.White;
CreateButton.BorderBrush = Brushes.Transparent;
CreateButton.Click += new RoutedEventHandler(Delete_OnClick);
MainGrid.Children.Add(CreateButton);
Grid.SetRow(CreateButton, ButtonLocation);
#endregion
}
private void Delete_OnClick(object sender, RoutedEventArgs e)
{
Button SelectedButton = (Button)sender;
int SelectedRow = Grid.GetRow(SelectedButton);
string[] Number = SelectedButton.Name.Split('_');
string TextBoxName = "NewTextBox" + "_" + Number[1];
TextBox SelectedTextbox = (TextBox)LogicalTreeHelper.FindLogicalNode(MainGrid, TextBoxName);
MainGrid.Children.Remove(SelectedTextbox);
MainGrid.Children.Remove(SelectedButton);
//Numberic--;
MainGrid.RowDefinitions.RemoveAt(SelectedRow);
}
XAML Code:
<Grid Name="MainGrid" ShowGridLines="True" OpacityMask="Black" Background="#FFEDEDED">
<Grid.RowDefinitions>
<RowDefinition Height="59" />
</Grid.RowDefinitions>
<Button Content="+" Height="35" HorizontalAlignment="Left" Margin="12,12,0,0" Name="NewButton" VerticalAlignment="Top" Width="35" BorderBrush="{x:Null}" Foreground="White" Background="#FF727272" FontWeight="Bold" FontSize="20" Click="NewButton_Click" />
</Grid>
Thank you, Tkay
Grid, or some other container and change it'sVisibilityproperty toCollapsedwhen the user doesn't need to see it.Textbox/Gridis as simple as removing an object from a collection.