抱歉,如果下面的代码格式不正确。我从手机上发帖,因为目前无线网络出现问题。
我希望能够做出这样的事情(我知道这样是不可能的):
...
<RowDefinition Height="Auto" ActualHeight="{Binding RowHeight, Mode="OneWayToSource"}"/>
...
因此,基本上高度是自动的(因为其中的项目(例如Label)的可能数量是0-n-使用ItemsControl表示形式),但是我需要知道项目控件的确切高度(在这种情况下,它是行)以计算代表我的数据所需的“页面”数。就像ms字中的页面一样。
但是,无法命名行(theRow.ActualHeight),因为通常不应该以这种方式完成VM/MVVM(无论如何我都无法直接访问 View )
知道怎么做吗?谢谢。
最佳答案
这就是我所拥有的,并且似乎有效。另外,我复制了在this帖子中找到的SizeObserver
:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="This is row 1" Grid.Row="0"></TextBlock>
<ItemsControl Grid.Row="1"
vm:SizeObserver.Observe="True"
vm:SizeObserver.ObservedHeight="{Binding ActHeight, Mode=OneWayToSource}">
<TextBlock Text="1"></TextBlock>
<TextBlock Text="2"></TextBlock>
<TextBlock Text="3"></TextBlock>
<TextBlock Text="4"></TextBlock>
<TextBlock Text="5"></TextBlock>
<TextBlock Text="6"></TextBlock>
</ItemsControl>
<TextBlock Text="This is row 3"
Grid.Row="2"></TextBlock>
</Grid>
在我看来,模型:
public double ActHeight
{
get
{
return _height;
}
set
{
_height = value;
}
}
因为
ItemsControl
是第1行中唯一的元素,所以我们知道行定义或行的高度将是该行内控件的实际高度ItemsControl
。
关于c# - 在VM中跟踪自动高度行的ActualHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174746/