即使将滚动条放入滚动查看器后,滚动条也不会显示在堆栈面板中

scrollbar not showing in stackpanel even after putting it inside a scrollviewer

我正在尝试将滚动查看器添加到堆栈面板 - "gradpaneldesc" 、 "undergrapaneldesc" 和 "certficatedesc" 分别位于另一个父堆栈面板 - "mainpanel" 内,但我无法实现这一点,因为它可能向我展示了滚动查看器,但没有实际的滚动条,没有意识到错误在哪里。任何帮助将不胜感激。

XAML

<UserControl x:Class="ISTE.Views.CoursesView"XAML
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ISTE.Views"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="1300">
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400"/>
            <ColumnDefinition Width="400"/>
            <ColumnDefinition Width="400"/>
        </Grid.ColumnDefinitions>
        <Image Source="/Images/degree.jpg"
                         Stretch="Fill"
                         Height="250"

                Grid.ColumnSpan="3"/>

        <StackPanel x:Name="mainpanel1" Grid.Column="0">
            <Button x:Name="Graduate" IsDefault="True" Click="Graduate_Click"   >
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FF235DF0" Offset="0"/>
                        <GradientStop Color="#FFE7EBF5" Offset="1"/>
                        <GradientStop Color="#FF9EB6F3" Offset="0.632"/>
                    </LinearGradientBrush>
                </Button.Background> Graduate
            </Button>
            <StackPanel x:Name="gradpanel" Height="200px"></StackPanel>
            <Border x:Name="myBorder2">
                <ScrollViewer>
                    <StackPanel x:Name="gradpaneldesc" Height="200px"></StackPanel>
                    </ScrollViewer>
            </Border>


        </StackPanel>
        <StackPanel x:Name="mainpanel2" Grid.Column="1">
            <Button x:Name="UnderGraduate" IsDefault="True" Click="UnderGraduate_Click"   >
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FF235DF0" Offset="0"/>
                        <GradientStop Color="#FFE7EBF5" Offset="1"/>
                        <GradientStop Color="#FF9EB6F3" Offset="0.632"/>
                    </LinearGradientBrush>
                </Button.Background> Undergraduate</Button>
            <StackPanel x:Name="undergradpanel" Height="200px"></StackPanel>
            <Border  x:Name="myBorder1">
                <ScrollViewer>
                    <StackPanel x:Name="undergradpaneldesc" Height="200px"></StackPanel>
                    </ScrollViewer>
            </Border>

        </StackPanel>
        <StackPanel Grid.Column="2">
            <Button x:Name="Certifications" IsDefault="True"  Click="Certifications_Click"    >
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FF235DF0" Offset="0"/>
                        <GradientStop Color="#FFE7EBF5" Offset="1"/>
                        <GradientStop Color="#FF9EB6F3" Offset="0.632"/>
                    </LinearGradientBrush>
                </Button.Background> Certifications</Button>
            <StackPanel x:Name="certpanel" Height="200px"></StackPanel>
            <Border  x:Name="myBorder3">
                <ScrollViewer>
                    <StackPanel x:Name="certificatedesc" Height="200px"></StackPanel>
                    </ScrollViewer>
            </Border>

        </StackPanel>

    </Grid>

CS

using ISTE.Models;
using ISTE.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace ISTE.Views
{
    /// <summary>
    /// Interaction logic for CoursesView.xaml
    /// </summary>
    public partial class CoursesView : UserControl
    {
        CoursesVM vm;
        public CoursesView()
        {
            InitializeComponent();
            vm = new CoursesVM();
            this.DataContext = vm;

            vm.GetData();
        }
        bool flag=false;
        bool flag1=false;
        private void UnderGraduate_Click(object sender1, RoutedEventArgs e1)
        {
            if (flag1 == false)
            {
                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    var textb = new Button();

                    undergradpanel.Children.Add(textb);
                    textb.Content = grad.degreeName;
                    textb.Click += new RoutedEventHandler(ugdegreeEventHandler);
                    Console.WriteLine("graddegrees \t" + grad.degreeName);
                }
            }
            flag1 = true;
        }
        void ugdegreeEventHandler(object sender2, RoutedEventArgs e2)
        {
            // Console.WriteLine("hi select");
            var b = sender2 as Button;
            //  var textb2 = new TextBlock();
            //  textb2.Text

                 myBorder1.Background = Brushes.SkyBlue;
                 myBorder1.BorderBrush = Brushes.Blue;
                 myBorder1.BorderThickness = new Thickness(2);
                 myBorder1.CornerRadius = new CornerRadius(8);
                 myBorder1.Padding = new Thickness(6);


            var degreeselected = b.Content.ToString();

            if (undergradpaneldesc.Children.Count < 1)
            {


                var textb2 = new TextBlock();
                            textb2.Name = "textblock";

                undergradpaneldesc.Children.Add(textb2);

                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        textb2.TextDecorations = TextDecorations.Underline;
                        textb2.Text = grad.description+"\n\n";

                        textb2.TextWrapping = TextWrapping.Wrap;
                        return;
                    }
                }


            }
            else
            {

                foreach (Undergraduate grad in vm.ItemData.undergraduate)
                {
                    if (grad.degreeName == degreeselected)
                    {

                        foreach (TextBlock ch in undergradpaneldesc.Children)
                        {
                            ch.Text = "";
                            ch.TextDecorations = TextDecorations.Underline;
                            ch.Text = grad.description+"\n\n";
                            ch.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                    }
                }
            }



        }
        private void Graduate_Click(object sender, RoutedEventArgs e)
        {

            if (flag == false)
            {
                foreach (Graduate grad in vm.ItemData.graduate)
                {


                        var textb = new Button();
                        gradpanel.Children.Add(textb);
                        textb.Content = grad.degreeName;
                        textb.IsDefault = true;
                        textb.Click += new RoutedEventHandler(degreeEventHandler);

                        Console.WriteLine("graddegrees \t" + grad.degreeName);

                }
            }
            flag = true;
        }

        void degreeEventHandler(object sender, RoutedEventArgs e)
        {
            // Console.WriteLine("hi select");
            var b = sender as Button;
            //  var textb2 = new TextBlock();
            //  textb2.Text

            var degreeselected = b.Content.ToString();

                myBorder2.Background = Brushes.SkyBlue;
                myBorder2.BorderBrush = Brushes.Black;
                myBorder2.BorderThickness = new Thickness(2);

                myBorder2.CornerRadius = new CornerRadius(8);
                myBorder2.Padding = new Thickness(6);


            if (gradpaneldesc.Children.Count<1)
            {


                var textb1 = new TextBlock();
                textb1.Name = "textblock1";

                gradpaneldesc.Children.Add(textb1);

                foreach (Graduate grad in vm.ItemData.graduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        if (grad.degreeName != "graduate advanced certificates")
                        {
                            textb1.Text = grad.description;
                            textb1.TextDecorations = TextDecorations.Underline;
                            foreach (var conc in grad.concentrations)
                            {
                                textb1.Text += conc + "\n\n";
                            }

                            textb1.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                        else
                        {

                            textb1.TextDecorations = TextDecorations.Underline;
                            foreach (var conc in grad.availableCertificates)
                            {
                                textb1.Text += conc + "\n\n";
                            }

                            textb1.TextWrapping = TextWrapping.Wrap;
                            return;
                        }
                    }
                }


            }
            else
            {

                foreach (Graduate grad in vm.ItemData.graduate)
                {
                    if (grad.degreeName == degreeselected)
                    {
                        if (grad.degreeName != "graduate advanced certificates")
                        {
                            foreach (TextBlock ch in gradpaneldesc.Children)
                            {
                                ch.Text = "";
                                ch.Text = grad.description + "\n\n";
                                ch.TextDecorations = TextDecorations.Underline;
                                foreach (var conc in grad.concentrations)
                                {
                                    ch.Text += conc + "\n";
                                }

                                ch.TextWrapping = TextWrapping.Wrap;
                                return;
                            }
                        }
                        else
                        {
                            foreach (TextBlock ch in gradpaneldesc.Children)
                            {
                                ch.Text = "";

                                ch.TextDecorations = TextDecorations.Underline;
                                foreach (var conc in grad.availableCertificates)
                                {
                                    ch.Text += conc + "\n";
                                }

                                ch.TextWrapping = TextWrapping.Wrap;
                                return;
                            }
                        }
                    }
                }
            }



        }

        private void Certifications_Click(object sender, RoutedEventArgs e)
        {
            var textb3 = new TextBlock();
            textb3.Name = "textblock3";

            certificatedesc.Children.Add(textb3);

            textb3.TextDecorations = TextDecorations.Underline;
            myBorder3.Background = Brushes.SkyBlue;
            myBorder3.BorderBrush = Brushes.Black;
            myBorder3.BorderThickness = new Thickness(2);

            myBorder3.CornerRadius = new CornerRadius(8);
            myBorder3.Padding = new Thickness(6);
            foreach (Graduate grad in vm.ItemData.graduate)
            {

                if (grad.degreeName == "graduate advanced certificates")
                {
                    textb3.Text = grad.description;
                    textb3.TextDecorations = TextDecorations.Underline;
                    foreach (var conc in grad.availableCertificates)
                    {
                        textb3.Text += conc + "\n\n";
                    }

                    textb3.TextWrapping = TextWrapping.Wrap;
                    return;
                }

            }
        }
    }
}

您需要指定 ScrollViewer 而不是 StackPanel 的高度,否则 StackPanel 将不会超出 ScrollViewer 和滚动条不显示。

<Border x:Name="myBorder2">
    <ScrollViewer Height="200px">
        <StackPanel x:Name="gradpaneldesc">
        </StackPanel>
    </ScrollViewer>
</Border>