【问题标题】:ListView DataTemplate Grid bind Image height to other View's heightListView DataTemplate Grid 将图像高度绑定到其他视图的高度
【发布时间】:2017-08-22 12:05:33
【问题描述】:

我有一个ListView 和这样的Grid DataTemplate 定义:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock x:Name="Tb" Text="{x:Bind Address}" TextWrapping="Wrap"/>
    <Image Source="{x:Bind SignalResource}" Height="{Binding ElementName=Tb, Path=ActualHeight}"/>
</Grid>

我想要ImageTextBlock's 的高度,但是我的Binding 不起作用(据我了解-因为当绑定解决后,View 的ActualHeight 仍然为0)

问题是如何将Image高度绑定到TextBlock高度?

【问题讨论】:

  • 这个问题能解决你的问题吗:stackoverflow.com/questions/2232675/… ?
  • @EamonnMcEvoy,不遗憾(如果我理解正确的话)。我偶然放置了有问题的WPF标签。我正在开发 UWP 应用,但 SharedSizeGroup 在那里不可用(仍然不确定)

标签: c# xaml uwp


【解决方案1】:

ActualHeight (Width) 不能用作常规属性 - MSDN information:

注意 虽然它有一个 ActualHeightProperty 支持字段,但 ActualHeight 不会引发属性更改通知,它应该被视为常规 CLR 属性而不是依赖项属性。

在您的情况下,您可以在 TextBlock 上使用 SizeChanged 事件,然后调整图像的高度。我不确定,但也许也可以通过 RelativePanel 进行正确对齐。这是 RelativePanel 的尝试(我已经尝试过,它有可能会起作用):

<RelativePanel>
    <TextBlock Name="MyTB" Text="Something" FontSize="24"/>
    <Rectangle Fill="Green" Width="40" RelativePanel.RightOf="MyTB" RelativePanel.AlignTopWith="MyTB" RelativePanel.AlignBottomWith="MyTB"/>
</RelativePanel>

【讨论】:

  • 我将您的答案用作奇怪而复杂的解决方案的一部分,并且它在结论中起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 2018-08-03
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多