【发布时间】:2014-01-20 01:14:28
【问题描述】:
我有 2 个按钮“添加标题”和“添加问题”。他们将单击“添加标题”,然后一个 ComboBox 和 TextBox 将如下所示:
从方法中可以看出,这些对象存储在 GroupBox 中:
C# 代码:
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
outerSp = new StackPanel() { Orientation = Orientation.Vertical };
sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
outerSp.Children.Add(sp);
gp.Content = outerSp;
spStandard.Children.Add(gp);
}
然后,当用户单击“添加问题”时,我需要将对象(组合框和文本框)添加到同一个 GroupBox 中的标题下。
这是添加问题的方法:
C# 代码:
private void ViewQuestions(StackPanel sp)
{
var stackPanel = gp.Content as StackPanel;
if (stackPanel != null)
{
stackPanel.Children.Add(sp);
}
else
gp.Content = sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
outerSp = new StackPanel() { Orientation = Orientation.Vertical };
sp = new StackPanel() { Orientation = Orientation.Horizontal };
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
outerSp.Children.Add(sp);
ViewQuestions(sp);
}
这是我尝试让问题对象出现在与标题相同的 GroupBox 中。此代码返回错误:
编辑:
这就是我想要实现的目标。
如果解释不够,请见谅。
【问题讨论】:
-
立即删除所有这些代码。使用
ItemsControl。不要在 WPF 的过程代码中创建或操作 UI 元素,这就是 XAML 的用途。 -
同意。你应该看看编写 C# 的 MVVM 模式:blogs.msdn.com/b/robertgreen/archive/2013/11/14/… 它很好地分离了 UI 和代码隐藏。在这种情况下,这似乎是您所需要的。
-
此外,要显示/隐藏各种控件,请在视图模型中使用布尔属性,然后通过
BooleanToVisibilityConverter将控件的Visibility属性绑定到此布尔值。无需即时生成控件。 -
@HighCore 抱歉,我忘了说我是在运行时创建这些的。这在 XAML 上仍然可行吗?
-
@SeanCogan 抱歉,我忘了说我是在运行时创建这些的。这在 XAML 上仍然可行吗?