我正在尝试在WPF中实现听起来很简单的东西,但是却无法解决这个问题。
我有一个ScrollViewer,其中包含两个GroupBox。第一个将其高度设置为固定值,第二个将采用窗口左侧的高度,但具有MinHeight。每个GroupBox都包含一个DataGrid。

我想做的是:
第二个groupbox的大小应设置为Window左侧的大小,而其中的DataGrid的大小应可填充该组框,并且如果不能全部显示行,则具有自己的滚动条。如果我将窗口的大小调整为小于GroupBox1.Height + GroupBox2.MinHeight,则滚动条应出现在窗口中。

我现在得到的行为是,第二个groupbox高度中的DataGrid随着行数的增加而增加,从而扩展了Groupbox并显示了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;
            }
        }
    }
}


我得到的是:

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

我想要什么(对不好的照片处理技能感到抱歉):

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

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

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

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

请注意,该示例非常简单,但是我要在其中进行操作的窗口要复杂得多,并且具有垂直滚动条比在此示例中更有意义。

谢谢!

最佳答案

您可以简单地将第二个MaxHeightGroupBox属性绑定到ActualHeight的容器的ScrollViewer减去第一个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/

10-13 08:20