【问题标题】:WPF. Bind to height of cell in GridViewColumn?WPF。绑定到 GridViewColumn 中单元格的高度?
【发布时间】:2012-03-20 06:12:12
【问题描述】:

我有一个这样定义的 GridView:

            <ListView x:Name="colorLegend" Background="Transparent" ItemsSource="{Binding /Colors}">
            <ListView.Resources>
                <Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                    <GridViewColumn Header="Image" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Border Background="{Binding Brush}" Height="20" Width="40" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Width="auto" />
                </GridView>
            </ListView.View>
        </ListView>

我希望第一个单元格内的边框高度与单元格列的高度一样高。 我可以将边框的高度绑定到第二个单元格的高度吗?

我想要的是能够更改第二个单元格的字体大小和第一个单元格的边框以调整大小以匹配更改?

谢谢!

【问题讨论】:

  • 你能详细说明一下使用一些快照吗?您来自第一列的Border 不是整个列的边框,而是应用于每个单元格,所以您知道。此外,您的第二列在视觉上也没有被识别为列,而只是一个垂直的单元格数组。因此,您需要详细说明您需要以图形方式实现的目标。
  • 好点!我需要改变我的问题。我的意思是 CELL 的高度

标签: wpf data-binding gridview datagridviewcolumn


【解决方案1】:

我终于通过将祖先类型指定给 GridViewPresenter 来实现它,就像这样。

<Border Background="{Binding Brush}" Width="40" Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GridViewRowPresenter}}, Path=ActualHeight}" />

【讨论】:

  • 与所有其他解决问题的方法不同,这对我有用。如果我可以进一步投票,我会的。
【解决方案2】:

在这种情况下,我宁愿使用VerticalAlignment=Stretch,而不是使用高度:

<DataTemplate>
    <Border Background="{Binding Brush}" VerticalAlignment="Stretch" Width="40" />
</DataTemplate>

这应该可以解决问题

(在这种情况下,您可能还需要一个 MinHeight,只是为了安全起见)

注意:VerticalAlignment 属性也可用于设置等价于 Height=* 的情况,这是一个常见问题. VerticalALignment=Stretch 或 Horizo​​ntalAlignment=Stretch 实际上非常有用,虽然不太为人所知,但遗憾的是。

编辑:如果这不起作用,您可能想尝试像这样绑定到父级:

<DataTemplate>
    <Border Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorLevel=1}}" Background="{Binding Brush}" Width="40" />
</DataTemplate>

(免责声明:我没有在 GridViewColumn 的 CellTemplate 中尝试过这个,所以如果它不起作用请不要拍我)

编辑 2: 如果以上都不起作用,请尝试在模板中插入 Grid 或 Panel,以便它继承高度并将其转发到 Border。 (就 Measure 继承而言,AFAIK、Grid 和 Panels 的行为不像边框):

<DataTemplate>
    <Grid>
        <Border Background="{Binding Brush}" VerticalAlignment="Stretch" Width="40" />
    </Grid>
</DataTemplate>

或:

<DataTemplate>
    <Grid Name="grid" VerticalAlignment="Stretch">
        <Border Background="{Binding Brush}" Width="40" Height="{Binding ActualHeight ElementName=grid}" />
    </Grid>
</DataTemplate>

【讨论】:

  • 我尝试设置 VerticalAlignment,但这不起作用。我似乎将高度设置为 0,因为没有显示边框。如果我使用 MinHeight,则始终使用该值。
  • arg,它在我的 dataGrid CellTemplate 中工作,我真的不明白为什么这在 GridView 中不起作用。无论如何,请参阅我的编辑以获取其他选项...
  • 我不会开枪的!承诺。 :) 无论如何,它没有工作。我收到一个错误,说我需要指定一个类型。我怎样才能弄清楚父母是什么类型?我试过 GridViewColumn 没有运气。
  • 啊,不知道这是什么类型,这就是我使用 AncestorLEvel 的原因,希望您不必提供类型……对此感到抱歉。我正在考虑“最后机会”选项,请参阅我的新编辑
  • 我要找出正确的anestortype,然后绑定ActualHeight,然后我就得到了想要的效果!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2016-09-23
  • 2017-09-14
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
  • 2011-02-08
  • 2016-05-06
相关资源
最近更新 更多