【发布时间】:2015-06-15 11:20:20
【问题描述】:
我已经用动态文本框和按钮测试了一些想法,将项目添加到我的网格效果很好,但是如果我想删除它有一些错误,有时 1 行是空的,我的添加按钮消失或我的程序崩溃.
我做错了什么或错过了什么?
C#代码:
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 代码:
<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>
谢谢你, 好了
【问题讨论】:
-
为什么以所有神圣的名义,您要动态添加/删除控件?考虑将您的控件包装在
Grid或其他一些容器 中,并在用户不需要看到它时将其Visibility属性更改为Collapsed。 -
你的意思是我为什么要添加和删除行?我在想有一个无尽的添加系统会很好。
-
我相信@MikeEason 的意思是你为什么像使用WinForms 一样使用WPF?。在 WPF 中,我们通常使用 XAML 和数据绑定,并且不从后面的代码中手动添加 UI 元素。如果您使用过数据绑定,那么删除数据绑定
Textbox/Grid就像从集合中删除对象一样简单。
标签: c# wpf button dynamic textbox