我在设置以下 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>
DataGrid1 控件没有显示任何定义了很多列和行的滚动条。 当我将 Height="Auto"替换为 Height="*"时,一切正常,水平和垂直滚动条显示如预期。
当我直接在 DataGrid1 上声明 MaxHeight 时它也有效,但这并不是我真正想要的。
这是子控件在设置 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; }
}
最佳答案
这正是你所说的。
之所以看不到 Scrollbar
,是因为即使 Grid
Clip 是 DataGrid
,它也只是一个 Clip, DataGrid
的 ActualHeight
是允许显示其所有子项时它将获得的高度。因此,您看不到它是滚动条的。 ActualHeight
之所以如此,是因为它可以通过 Grid
上的 Height="Auto"
获得所需的所有空间。 我个人不会将此称为错误的原因是,如果您想为某些动画使用 考虑到这一点,我实际上认为我也将其称为“不理想的功能”而不是“不正确的输出”方面的错误Grid
的 ClipToBounds
属性,您可能会希望这种行为这就是您想要的行为。
要获得您正在寻找的行为,
- 在
DataGrid
上应用MaxHeight
或使用 GridRowDefinition.Height="*"
<- 如您所述(不知道为什么你说这不是你想做的?) - 或者您也可以在
DataGrid
上使用RelativeSourceBinding
有点像-
<DataGrid Name="DataGrid1"
Grid.Row="1"
Height="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Grid}},
Path=RowDefinitions[1].ActualHeight}">
对于此类问题 Snoop 是你的 friend 。当您使用 Snoop 检查 DataGrid
上的 ActualHeight
并看到它为 child 控制。
关于c# - 自动高度结合 MaxHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742513/