【问题标题】:Adding objects to a GroupBox issue将对象添加到 GroupBox 问题
【发布时间】:2013-12-30 16:00:57
【问题描述】:

我正在尝试将不同的对象添加到 GroupBox。当我单击“添加标题”按钮时,我可以让组合框和文本框出现在 Groupbox 中。

我现在想要发生的是当他们单击“添加问题”时,我希望能够将问题对象(组合框、文本框)添加到与最后添加的标题相同的组框中。

C#代码:

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

【问题讨论】:

    标签: c# wpf runtime


    【解决方案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 中。代码未经测试我试图给你一个想法。我希望这会有所帮助。

    【讨论】:

    • 这是为了覆盖吗?因为我不希望它覆盖我只是希望它像在堆栈面板中一样出现在下方。
    • 但它需要在标题的分组框中?
    • 不是 gp 标题的分组框吗?
    • gp 是一个标题组框,但是当他们添加问题时,需要将其添加到 gp。
    • 您是否复制并粘贴了上面的代码。上面的代码将它添加到 Title groupBox 并且我期望 gp 是类级别的字段。
    猜你喜欢
    • 2014-01-20
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2019-12-03
    • 2014-09-30
    • 2015-06-17
    相关资源
    最近更新 更多