Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF不显示垂直滚动条_C#_Wpf - Fatal编程技术网

C# WPF不显示垂直滚动条

C# WPF不显示垂直滚动条,c#,wpf,C#,Wpf,我试图创建一个应用程序,根据从数据库收集的信息显示用户控件。问题是,如果项目的总高度超过为该显示保留的主窗口网格,我看不到垂直滚动条。从我发现的可能存在的问题来看,stackpanel/scrollviewer并没有限制其子级的大小。我一直在想如何解决这个问题,有没有办法在有限的空间里堆叠项目,滚动条只有在需要时才可见。 我想避免对scrollviewer的高度进行“硬编码”。 目前,我只能使用以下代码: XAML:(主窗口) 和任务项类(通过datacontext将项绑定到该类) 正如您所看到

我试图创建一个应用程序,根据从数据库收集的信息显示用户控件。问题是,如果项目的总高度超过为该显示保留的主窗口网格,我看不到垂直滚动条。从我发现的可能存在的问题来看,stackpanel/scrollviewer并没有限制其子级的大小。我一直在想如何解决这个问题,有没有办法在有限的空间里堆叠项目,滚动条只有在需要时才可见。 我想避免对scrollviewer的高度进行“硬编码”。 目前,我只能使用以下代码:

XAML:(主窗口)

和任务项类(通过datacontext将项绑定到该类)

正如您所看到的,我试图将scrollviewer高度绑定到我在它上面创建的网格,认为如果我将高度设置为“自动”,它将从网格实际高度获取高度。嗯,这没有多大意义,因此它不起作用。我正试图找到另一种对项目进行分组的解决方案,这样我就可以轻松地扩展主窗口,而无需对每个窗口进行编码以使用此扩展进行扩展,这样项目就可以填充所有可用空间并在需要时显示滚动条


请帮助:)

您需要在某个地方限制
ScollViewer
的高度。窗口中的
ucTaskItem
位于何处?ucTaskWindow停靠在主窗口上,而ucTaskItem停靠在ucTaskWindow上。我对其进行了一些修改,并尝试使用DockPanel执行此操作。不幸的是,我也遇到了同样的问题,有趣的是,DockPanel如果是宽度问题,效果会更好,但对高度不起作用。另外,我想在不声明高度的情况下实现它:)如前所述,高度需要约束在元素树的某个位置。
<Grid   Name="selectedOptionGrid"
        Grid.Column="1"
        Grid.Row="1"
        HorizontalAlignment="Left"
        VerticalAlignment="Top">
        <StackPanel Name="selectedOptionStack"/>
        <!-- Dock selected windows based on option selected-->
</Grid>
<UserControl x:Class="Power_Planner_1._0.Team_Planner.MyTasks.ucTaskWindow"
             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:Power_Planner_1._0.Team_Planner.MyTasks" 
             mc:Ignorable="d">
    <Grid Background="White"
          VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
                <RowDefinition Height="5"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0"
                        Orientation="Horizontal"
                        VerticalAlignment="Top"
                        Margin="0,5,0,0">
                        <TextBlock Margin="60,0,0,0" Width="160" FontWeight="Bold">Task Name</TextBlock>
                        <TextBlock Margin="10,0,0,0" Width="160" FontWeight="Bold">Team Name</TextBlock>
                        <TextBlock Margin="0,0,0,0" Width="55" FontWeight="Bold">Deadline</TextBlock>
                        <TextBlock Margin="20,0,0,0" Width="60" FontWeight="Bold">Start</TextBlock>
                        <TextBlock Margin="28,0,0,0" Width="60" FontWeight="Bold">End</TextBlock>
                        <TextBlock Margin="20,0,0,0" Width="60" FontWeight="Bold">Run Time</TextBlock>
        </StackPanel>
            
            <Separator  Grid.Row="1"
                        VerticalAlignment="Top"/>

            <Grid   Name="taskListGrid"
                    Grid.Row="2"
                    Height="510"
                    Width="830">
                <ScrollViewer   VerticalScrollBarVisibility="Auto"
                                Width="{Binding ActualWidth,RelativeSource={RelativeSource 
                                Mode=FindAncestor, AncestorType=Grid}}"                   
                                Height="{Binding ActualHeight,RelativeSource={RelativeSource
                                Mode=FindAncestor, AncestorType=Grid}}">
                    <StackPanel Orientation="Vertical"
                                    x:Name="taskListStack">
                        <StackPanel.Resources>
                            <Style TargetType="UserControl">
                                <Setter Property="Margin" Value="0,5,0,0"/>
                            </Style>
                        </StackPanel.Resources>
                    </StackPanel>
                </ScrollViewer>
            </Grid>



    </Grid>
</UserControl>
<UserControl x:Class="Power_Planner_1._0.Team_Planner.MyTasks.ucTaskItem"
             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:Power_Planner_1._0.Team_Planner.MyTasks"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d" d:DesignHeight="45">
    <Border CornerRadius="12,12,12,12"
            BorderThickness="1,1,1,1"
            BorderBrush="LightGray"
            Background="#FF456780"
            Padding="0">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="160"/>
                <ColumnDefinition Width="160"/>
                <ColumnDefinition Width="65"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="65"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <!--Picture-->
                <Border     Grid.Column="0"
                            CornerRadius="25,25,25,25"
                            BorderThickness="1,1,1,1"
                            Width="30"
                            Height="30"
                            BorderBrush="LightGray"
                            Background="ForestGreen"
                            HorizontalAlignment="Left"
                            Margin="10,0,0,0">
                </Border>
                <!--Task Name-->
                <TextBlock  Grid.Column="1"
                            Margin="10,0,0,0"
                            FontSize="12"
                            Foreground="LightGray"
                            Text="{Binding taskName}"
                            Width="160"
                            VerticalAlignment="Center">
                </TextBlock>
                <!--Team Name-->
                <TextBlock  Grid.Column="2"
                            Margin="20,0,0,0"
                            FontSize="12"
                            Foreground="LightGray"
                            Text="{Binding teamName}"
                            Width="160"
                            VerticalAlignment="Center">
                </TextBlock>
                <!--Deadline-->
                <TextBlock  Grid.Column="3"
                            Margin="20,0,0,0"
                            FontSize="12"
                            Foreground="LightGray"
                            Text="{Binding deadline}"
                            Width="45"
                            VerticalAlignment="Center">
                </TextBlock>
                <!--Start-->
                <Button Grid.Column="4"
                            Background="#FF456780"
                            BorderBrush="LightGray"
                            Height="25"
                            Margin="20,0,0,0"
                            Width="60">
                    <Button.Resources>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="CornerRadius" Value="10"/>
                        </Style>
                    </Button.Resources>
                <TextBlock Foreground="LightGray">Start</TextBlock>
                </Button>
                <!--Stop-->
                <Button Grid.Column="5"
                            Background="#FF456780"
                            BorderBrush="LightGray"
                            Height="25"
                            Margin="20,0,0,0"
                            Width="60">
                    <Button.Resources>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="CornerRadius" Value="10"/>
                        </Style>
                    </Button.Resources>
                    <TextBlock Foreground="LightGray">Stop</TextBlock>
                </Button>
                <!--Deadline-->
                <TextBlock  Grid.Column="6"
                            Margin="20,0,0,0"
                            FontSize="12"
                            Foreground="LightGray"
                            Text="{Binding runtime}"
                            Width="45"
                            VerticalAlignment="Center">
                </TextBlock>
                <!--Other Options-->
                <materialDesign:PopupBox    Grid.Column="7"
                                            StaysOpen="False" 
                                            Foreground="White"
                                            HorizontalAlignment="Right">
                    <StackPanel Width="150"
                                Background="White">
                        <Button Content="Edit"/>
                        <Button Content="View Details"/>
                        <Button Content="Comment"/>
                    </StackPanel>
                </materialDesign:PopupBox>
        </Grid>
    </Border>

</UserControl>
taskListStack.Children.Clear();
var task1 = new clsTaskItem("Test1", "blablabla", "12:40", "02:30");
taskListStack.Children.Add(new ucTaskItem(task1));
var task2 = new clsTaskItem("Test2", "blablabla", "13:45");
taskListStack.Children.Add(new ucTaskItem(task2));
var task3 = new clsTaskItem("Test3", "blablabla", "11:45");
taskListStack.Children.Add(new ucTaskItem(task3));
var task4 = new clsTaskItem("Test4", "blablabla", "14:45");
taskListStack.Children.Add(new ucTaskItem(task4));
var task5 = new clsTaskItem("Test5", "blablabla", "17:45");
taskListStack.Children.Add(new ucTaskItem(task5));
var task6 = new clsTaskItem("Test6", "blablabla", "18:45");
taskListStack.Children.Add(new ucTaskItem(task6));
var task7 = new clsTaskItem("Test7", "blablabla", "13:23");
taskListStack.Children.Add(new ucTaskItem(task7));
var task8 = new clsTaskItem("Test8", "blablabla", "12:54");
taskListStack.Children.Add(new ucTaskItem(task8));
var task9 = new clsTaskItem("Test9", "blablabla", "17:23");
taskListStack.Children.Add(new ucTaskItem(task9));
var task10 = new clsTaskItem("Test10", "blablabla", "17:10");
taskListStack.Children.Add(new ucTaskItem(task10));
    taskListStack.Children.Add(new ucTaskItem(task1));
    taskListStack.Children.Add(new ucTaskItem(task2));
    taskListStack.Children.Add(new ucTaskItem(task3));
    taskListStack.Children.Add(new ucTaskItem(task4));
    taskListStack.Children.Add(new ucTaskItem(task5));
    taskListStack.Children.Add(new ucTaskItem(task6));
    taskListStack.Children.Add(new ucTaskItem(task7));
    taskListStack.Children.Add(new ucTaskItem(task8));
    taskListStack.Children.Add(new ucTaskItem(task9));
    taskListStack.Children.Add(new ucTaskItem(task10));
public class clsTaskItem
{
    public clsTaskItem(string tsname, string tmname, string dline, string rtime = "00:00:00")
    {
        taskName = tsname;
        teamName = tmname;
        deadline = dline;
        runtime = rtime;
    }

    public string taskName  { get; private set; }
    public string teamName  { get; private set; }
    public string deadline  { get; private set; }
    public string runtime   { get; private set; }
}