自动高度与MaxHeight结合使用

Ran*_*dom 16 c# wpf xaml

我在设置以下xaml布局时遇到问题:

RowHeightAuto.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="GridMaxHeight.RowHeightAuto"
    Title="RowHeightAuto" WindowState="Maximized">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" MaxHeight="200" />
    </Grid.RowDefinitions>

    <StackPanel Background="LightGray" Grid.Row="0"></StackPanel>
    <DataGrid Name="DataGrid1" Grid.Row="1" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

DataGrid1控件未显示任何定义了大量列和行的滚动条.当我用高度="*"替换高度="自动"时,一切都有效,而水平和垂直滚动条看起来像预期的那样.

当我直接在DataGrid1声明MaxHeight时它也可以工作,但这并不是我想要的.

这是一个bug控件,即childcontrol在设置Height ="Auto"时忽略了最大高度,或者我是否可能做错了?使用ListBox/ListView等可以重现相同的行为,也可以使用ComponentOne,Telerik等第三方控件...

如果这是一个错误 - 你知道一个解决方法或有其他提示吗?

这是我如何设置DataGrid的ItemsSource的代码.RowHeightAuto.xaml.cs

public partial class RowHeightAuto : Window
{
    private readonly DateTime _start;

    public RowHeightAuto()
    {
        InitializeComponent();

        DataGrid1.ItemsSource = GetTestData();

        _start = DateTime.Now;
        Dispatcher.BeginInvoke(new Action(() => MessageBox.Show((DateTime.Now - _start).TotalSeconds.ToString(CultureInfo.InvariantCulture))), DispatcherPriority.ContextIdle, null);
    }

    public static List<TestData> GetTestData()
    {
        const int maxCols = 501;
        const int maxRows = 300;

        var testDatas = new List<TestData>(maxRows);
        for (int i = 0; i < maxRows; i++)
            testDatas.Add(new TestData());

        for (int i = 0; i < maxCols; i++)
        {
            string propName = string.Format("Property{0}", AddLeadingZeros(i));

            for (int j = 0; j < maxRows; j++)
                testDatas[j][propName] = propName;
        }

        return testDatas;
    }

    private static string AddLeadingZeros(int val)
    {
        return val.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0');
    }
}

public class TestData
{
    public object this[string propertyName]
    {
        get
        {
            var myType = GetType();
            var myPropInfo = myType.GetProperty(propertyName);
            return myPropInfo.GetValue(this);
        }
        set
        {
            var myType = GetType();
            var myPropInfo = myType.GetProperty(propertyName);
            myPropInfo.SetValue(this, value, null);

        }
    }

    public string Property000 { get; set; }
    public string Property001 { get; set; }
    public string Property002 { get; set; }
    public string Property003 { get; set; }
    ...
    public string Property498 { get; set; }
    public string Property499 { get; set; }
    public string Property500 { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

Viv*_*Viv 10

这正如你所说的那样.

为什么你没有看到的原因Scrollbar的是因为即使Grid剪辑的DataGrid,它只是一个片段,该ActualHeightDataGrid是高度,如果允许以显示它的所有的孩子会得到.因此,你没有看到它的滚动条.ActualHeight如此COS它允许让所有它想要的空间Height="Auto"Grid.我不会把这样的个人错误的原因是因为如果你想用玩你可能希望这种行为ClipToBounds的性质Grid为某些动画,这是你想要的行为.考虑到这一点,我实际上认为我称之为"不理想的功能"而不是"不正确的输出"

为了得到你想要的行为,

  • 应用MaxHeighton DataGrid或使用Grid RowDefinition.Height="*"< - 如你所提到的那样(不确定为什么你说这不是你想做的?)
  • 或者你也可以使用RelativeSourceBindingDataGrid

就像是 -

<DataGrid Name="DataGrid1" 
          Grid.Row="1"
          Height="{Binding RelativeSource={RelativeSource FindAncestor,
                           AncestorType={x:Type Grid}},
                           Path=RowDefinitions[1].ActualHeight}">
Run Code Online (Sandbox Code Playgroud)

对于这些问题,Snoop是你的朋友.您可以轻松地检查这种行为,并明白为什么滚动条的未显示当您检查ActualHeightDataGrid使用snoop,看它分配相当多的高度与子控件.