c# - 在VM中跟踪自动高度行的ActualHeight

标签 c# wpf mvvm itemscontrol

抱歉,如果下面的代码格式不正确。我从手机上发帖,因为目前无线网络出现问题。

我希望能够做出这样的事情(我知道这样是不可能的):

...
<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/

相关文章:

c# - 使用 .NET (C#) 修改路由表

wpf - ContentControl 崩溃中的绑定(bind)

mvvm - RxSwift : How to pass a control event through multiple view models

c# - 首先使用带有 EF 代码的存储库模式时的 Linq 子查询

c# - 自定义 ValidationAttribute 未在 View 模型中触发 IsValid 函数调用

c# - 序列化和反序列化域事件以在通用实现中持久化并从事件存储中检索

c# - WPF、Caliburn Micro 和 Dapper - Datagrid 复选框绑定(bind)

c# - 相对定位 child 的容器

wpf - 如何使用 ItemContainerStyle 设置 MenuItem 的图标

wpf - 如何将 MenuItem 的数据绑定(bind)列表合并到 WPF 中的另一个 MenuItem?