【问题标题】:Arranging in order with reference to a column参照列按顺序排列
【发布时间】:2013-07-11 03:14:57
【问题描述】:

我有一个 MySQL 数据库表(我正在使用实体框架),如下所示:

这是我用来在 WPF 上检索和填充它们的代码: CRUD 类文件:

        //Get all records based on ActivityID and TaskID.
    public IList<Model.questionhint> GetRecords(int listTask, int listActivity)
    {
        IList<Model.questionhint> lstRecords = context.questionhints.ToList();
        return lstRecords.Where(a => a.TaskID == listTask && a.ActivityID == listActivity).ToList(); 

    }

后面的代码:

      public MainWindow2()
    {
        InitializeComponent();
        PopulateQuestion(1, 5);
    }

    private void PopulateQuestion(int activityID, int taskID)
    {
        IList<Model.questionhint> lstQuestionHints = qh.GetRecords(taskID, activityID);

        foreach(Model.questionhint qhm in lstQuestionHints)
        {

            TextBlock tb = new TextBlock();
            tb.Text = qhm.QuestionContent;              
            tb.FontWeight = FontWeights.Bold;
            tb.FontSize = 24;
            WrapPanelTest.Children.Add(lbl);

            if (qhm.Option1.Trim().Length > 0 &&
                qhm.Option2.Trim().Length > 0)
            {
                ComboBox cb = new ComboBox();
                cb.Items.Add(qhm.Option1);
                cb.Items.Add(qhm.Option2);
                cb.Width = 200;
                WrapPanelTest.Children.Add(cb);
            }

        }
    }

它如何出现在我的程序中:

如您所见,问题都是复合在一起的,我想根据上面数据库表中的 questionNo 将它们分开(例如,具有相同 questionNo 的几条记录应该复合在一起。)但我完全不知道如何 。我想将它们分开:

我[comboBox]每天下午要小睡一下。

太阳 [comboBox] 不会绕地球转。

感谢您对此的任何帮助,在此先感谢。

【问题讨论】:

    标签: c# mysql wpf database entity-framework


    【解决方案1】:

    试试这个

         StackPanel sp=new StackPanel();
        foreach(Model.questionhint qhm in lstQuestionHints)
        {
        StackPanel sp1=new StackPanel(){Orientation=Orientation.Horizontal};     
            TextBlock tb = new TextBlock();
            tb.Text = qhm.QuestionContent;              
            tb.FontWeight = FontWeights.Bold;
            tb.FontSize = 24;
            sp1.Children.Add(lbl);
    
            if (qhm.Option1.Trim().Length > 0 &&
                qhm.Option2.Trim().Length > 0)
            {
                ComboBox cb = new ComboBox();
                cb.Items.Add(qhm.Option1);
                cb.Items.Add(qhm.Option2);
                cb.Width = 200;
                sp1.Children.Add(cb);
            }
           sp.Children.Add(sp1); 
        }
        WrapPanelTest.Children.Add(sp);
    

    【讨论】:

    • 您好,感谢您花时间帮助我,非常感谢。现在每条记录都被逐行分隔。我想我必须更改我的查询,以便将同一问题的记录分组在一起。
    【解决方案2】:

    在 StackPanel 而不是 WrapPanel 中添加您的控件。

    【讨论】:

    • 为什么?抱歉,WPF 新手,我不太确定每个面板的作用
    • StackPanel 将其子水平和垂直排列,检查其属性 Orientation。默认情况下它是垂直的。 WrapPanel 如果孩子的宽度超出可用空间,则会包裹孩子,并且只会水平排列孩子。
    • 所以你需要一个 StackPanel 来添加你的 TextBlock 和 ComboBox 并且它的方向应该是水平的。然后将此 StackPanel 添加到 Orintation 为 Vertical 的另一个 StackPanel。
    • 我只有一个覆盖整个窗口的 WrapPanel。可以在后面的代码中添加/创建面板吗?
    • 使用 WrapPanel 作为根容器不是一个好习惯,您最好使用 Grid 作为根容器。是的,您在 XAML 中所做的任何事情也可以在代码后面完成。
    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多