c# - 在WPF中如何防止ScrollViewer内的控件展开

标签 c# wpf datagrid scrollviewer

我试图在 WPF 中实现一些听起来很简单的东西,但就是无法回避这样做。 我有一个 ScrollViewer,其中包含两个 GroupBox。第一个将其高度设置为固定值,第二个将采用窗口的剩余部分,但有一个 MinHeight。每个 GroupBox 都包含一个 DataGrid。

我想做的是: 第二个组框的大小应调整为窗口左侧的大小,并且其内部的 DataGrid 的大小应调整为填充组框,并且如果无法全部显示行,则应有自己的滚动条。如果我将窗口大小调整为小于 GroupBox1.Height+GroupBox2.MinHeight,则窗口中应出现滚动条。

我现在得到的行为是,第二个组框的 DataGrid 高度随着行数的增加而增加,从而扩展组框并显示 Scrollviewer 的滚动条。

我想出了一个小演示应用程序来展示这种行为

WPF:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="400"
    Width="500">
<Grid>
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <GroupBox Header="test1"
                      Grid.Row="0">
                <DataGrid ItemsSource="{Binding Colors}">
                </DataGrid>
            </GroupBox>
            <GroupBox Header="test2"
                      Grid.Row="1"
                      MinHeight="50">
                <DataGrid ItemsSource="{Binding Colors}">
                </DataGrid>
            </GroupBox>
        </Grid>

    </ScrollViewer>
</Grid>

C#

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            Colors = new List<Color>();
            for (int i = 1; i < 51; i++)
            {
                byte b = (byte)(i * 5);
                Colors.Add(Color.FromRgb(b,b,b));
            }
        }

        private List<Color> _colors;
        public List<Color> Colors
        {
            get
            {
                return _colors;
            }
            set
            {
                _colors = value;
            }
        }
    }
}

我得到了什么:

What i'm getting

我想要什么(抱歉照片处理技巧不好):

What i would want

除非,如前所述,我将窗口大小调整为小于 group1 的固定大小和 group2 的最小大小之和,在这种情况下,我需要窗口的滚动条。

在这种情况下,我希望它看起来像这样:(又是一个模型,而不是实际的屏幕截图)

Mockup2

请注意,这个示例非常简单,但我尝试在其中执行此操作的窗口要复杂得多,并且使用垂直滚动条比本示例中的垂直滚动条更有意义。

谢谢!

最佳答案

您只需将第二个 GroupBoxMaxHeight 属性绑定(bind)到 ScrollViewer 容器的 ActualHeight 即可> 减去第一个 GroupBox

完整示例(不包括与您的代码相同的代码。):

<Window.Resources>
    <wpfApp1:SubtractConverter x:Key="SubtractConverter"/>
</Window.Resources>

<Grid Name="Root">
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <GroupBox
                Name="Test1"
                Header="test1"
                Grid.Row="0">

                <DataGrid ItemsSource="{Binding Colors}"/>
            </GroupBox>
            <GroupBox
                Header="test2"
                Grid.Row="1"
                MinHeight="250">
                <DataGrid ItemsSource="{Binding Colors}"/>

                <GroupBox.MaxHeight>
                    <MultiBinding Converter="{StaticResource SubtractConverter}">
                        <Binding Path="ActualHeight" ElementName="Root"/>
                        <Binding Path="ActualHeight" ElementName="Test1"/>
                    </MultiBinding>
                </GroupBox.MaxHeight>
            </GroupBox>
        </Grid>
    </ScrollViewer>
</Grid>

public class SubtractConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double[] doubles = values.Cast<double>().ToArray();

        double result = doubles[0];

        for (int i = 1; i < doubles.Length; i++)
        {
            result -= doubles[i];
        }

        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

关于c# - 在WPF中如何防止ScrollViewer内的控件展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736374/

相关文章:

c# - 当我在 DataGridTemplateColumn.CellTemplate 中使用 TextBox 时,DataGrid.BeginningEdit 事件未触发

c# - 将 NLog 与 NServiceBus 3 一起使用

c# - WPF 用户控件动画不会工作..?

c# - WPF 应用程序中的 MessageBox 仍然显示没有样式

silverlight - 使用 M-V-VM Light 根据 Silverlight 4 中的业务规则标准标记 DataGrid 行

wpf - 如何通过触发器更改 IsReadOnly

c# - 当一个线程调用 WaitOne 时,如何找到发回信号的其他线程?

c# - 如何在 C# 中有条件地转换为多种类型

c# - 电子邮件未在 azure 网络应用程序中发送

c# - 在WPF C#中的wrappanel内部重新排列CustomControl