Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
WPF DataGrid需要网格行中的垂直滚动条_Wpf_Datagrid_Vertical Scrolling - Fatal编程技术网

WPF DataGrid需要网格行中的垂直滚动条

WPF DataGrid需要网格行中的垂直滚动条,wpf,datagrid,vertical-scrolling,Wpf,Datagrid,Vertical Scrolling,我的客户正在开发一个属性编辑器,其中包含多个属性部分,这些属性部分以WPF扩展器的形式呈现,其中包含数据网格。我们在WPF网格中布局了所有内容,目前具有自动大小的行 我们试图解决的问题是当屏幕高度改变时,每个属性部分的大小成比例。当扩展器打开时,它们应该占据一定比例的空间,垂直滚动条会在需要时自动启动。当它们关闭时,它们应该折叠起来,并为剩余的部分提供剩余的空间 在现实生活中,我们有4个内容扩展器。第一个是固定高度,其余三个是列表和数据网格,需要按比例调整大小,最后一个是剩余空间的最大部分。请记

我的客户正在开发一个属性编辑器,其中包含多个属性部分,这些属性部分以WPF扩展器的形式呈现,其中包含数据网格。我们在WPF网格中布局了所有内容,目前具有自动大小的行

我们试图解决的问题是当屏幕高度改变时,每个属性部分的大小成比例。当扩展器打开时,它们应该占据一定比例的空间,垂直滚动条会在需要时自动启动。当它们关闭时,它们应该折叠起来,并为剩余的部分提供剩余的空间

在现实生活中,我们有4个内容扩展器。第一个是固定高度,其余三个是列表和数据网格,需要按比例调整大小,最后一个是剩余空间的最大部分。请记住,用户可能会调整屏幕大小,或者用户可能以不同的屏幕分辨率运行,因此我们需要它做出相应的反应

我们已经尝试过处理DataGrid行定义高度(固定、比例和自动)和MaxHeight,以及每个DataGrid的MaxHeight,但我似乎找不到一个组合,在需要时会消耗整个区域。我们正在研究一种基于代码的解决方案,但我们很好奇其他人会提出什么建议

下面是一个简单的代码示例,它将为您提供我们所追求的布局。我们只需要它如上所述工作

以下是代码(这将是真实世界中的视图模型)

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
datagrid1.DataContext=GetCustomerVM();
datagrid2.DataContext=GetCustomerVM();
datagrid3.DataContext=GetCustomerVM();
}
私有列表GetCustomerVM()
{
var CustomerList=新列表();
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
添加(新客户(){FirstName=“A”});
返回客户列表;
}
}
公共类客户
{
公共字符串名{get;set;}
}
XAML


就我所能理解的您的需求而言,我认为这符合您的需求:

  • 每个扩展器在未扩展时,都会占用所需的最小空间
  • 展开时,顶行的大小与其内容相符
  • 第二行、第三行和第四行分割空间的其余部分。当膨胀时,它们会拉伸。两个得到一份可用空间,三个得到两份,四个得到三份。如果两个和三个是开放的,两个得到四份股票中的一份,三个得到另外三份
这些份额由触发器中的设置者分发。这是第四行的触发器:

<DataTrigger
    Binding="{Binding Children[3].IsExpanded, RelativeSource={RelativeSource AncestorType=Grid}}"
    Value="True"
    >
    <Setter Property="Height" Value="3*" />
</DataTrigger>

在XAML中这样做的好处是,它永远不会给您带来任何丑陋的惊喜

下面是XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Setter Property="Height" Value="Auto" />
                    <Style.Triggers>
                        <DataTrigger
                            Binding="{Binding Children[1].IsExpanded, RelativeSource={RelativeSource AncestorType=Grid}}"
                            Value="True"
                            >
                            <Setter Property="Height" Value="1*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Setter Property="Height" Value="Auto" />
                    <Style.Triggers>
                        <DataTrigger
                            Binding="{Binding Children[2].IsExpanded, RelativeSource={RelativeSource AncestorType=Grid}}"
                            Value="True"
                            >
                            <Setter Property="Height" Value="2*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Setter Property="Height" Value="Auto" />
                    <Style.Triggers>
                        <DataTrigger
                            Binding="{Binding Children[3].IsExpanded, RelativeSource={RelativeSource AncestorType=Grid}}"
                            Value="True"
                            >
                            <Setter Property="Height" Value="3*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
    </Grid.RowDefinitions>
    <Expander
        Grid.Row="0"
        VerticalAlignment="Stretch"
        Header="One" Background="LightGreen">
        <StackPanel>
            <Label>Content One</Label>
            <Label>Content One</Label>
            <Label>Content One</Label>
        </StackPanel>
    </Expander>
    <Expander
        Grid.Row="1"
        VerticalAlignment="Stretch"
        Header="Two" Background="LightSkyBlue">
        <StackPanel>
            <Label>Content Two</Label>
            <Label>Content Two</Label>
            <Label>Content Two</Label>
        </StackPanel>
    </Expander>
    <Expander
        Grid.Row="2"
        VerticalAlignment="Stretch"
        Header="Three" Background="LightGoldenrodYellow">
        <StackPanel>
            <Label>Content Three</Label>
            <Label>Content Three</Label>
            <Label>Content Three</Label>
        </StackPanel>
    </Expander>
    <Expander
        Grid.Row="3"
        VerticalAlignment="Stretch"
        Header="Four" Background="Khaki">
        <StackPanel>
            <Label>Content Four</Label>
            <Label>Content Four</Label>
            <Label>Content Four</Label>
        </StackPanel>
    </Expander>
</Grid>

内容一
内容一
内容一
内容二
内容二
内容二
内容三
内容三
内容三
内容四
内容四
内容四
这些不会显示滚动条,因为我没有花时间创建将滚动的内容


Ed,这看起来是解决方案。我不得不用多触发器替换你的触发器,因为我忘了提到最后一个部分可以由用户通过在这个控件之外的屏幕的另一个区域中的操作将可见性设置为折叠。因此,在我们的版本中,Grid3的触发器同时关注Grid4的IsExpanded和Visibility属性。谢谢你的帮助。如果我们有问题,我会报告的。@KyleLib真棒。祝你好运