将对象添加到GroupBox问题

时间:2013-12-30 16:00:58

标签: c# wpf runtime

我正在尝试向GroupBox添加不同的对象。当我点击“添加标题”按钮时,我可以将组合框和文本框显示在组框中。

我现在想要的是当他们点击“添加问题”时,我希望能够将问题对象(Combobox,Textbox)添加到与最后添加的标题相同的Groupbox中。

C#CODE:

private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{

        CurrentSortItem++;
        SortItems.Add(CurrentSortItem);

        StackPanel 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);

        gp.Content = sp;



        spStandard.Children.Add(gp);

}

private void ViewQuestions(StackPanel sp)
{
        gp.Content = sp;
}

    List<int> SortItems1 = new List<int>();
    int CurrentSortItem1 = 0;
    int Count = 0;

private void btnQuestion_Click(object sender, RoutedEventArgs e)
{

        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);

            StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };



                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);

                ViewQuestions(sp);

    }

我试图尝试过,但是当我点击“添加问题”时,它只会覆盖标题的对象。

任何帮助都会很棒。

我正在使用WPF,这些是在运行时创建的。

感谢。

EDIT4:

我不得不搬家

StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
GroupBox gp;

正好在

之下
public partial class CreateNewStandard : Page
{

因此所有方法都可以看到它。然后我收到了这个错误。

http://i.stack.imgur.com/f9uqF.png

1 个答案:

答案 0 :(得分:1)

试试这个

private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{

    CurrentSortItem++;
    SortItems.Add(CurrentSortItem);

    StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
    StackPanel 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);

}

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)
{

    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);

}

你正在覆盖它。虽然你需要将它们添加到现有的stackPanel中。代码未经过测试我试图给你一个想法。我希望这会有所帮助。