如何获取 GridViewColumn 的实际宽度
我有以下 XAML:
<GridView x:Key="myGridView">
<GridViewColumn CellTemplate="{StaticResource myTemplate}"/>
</GridView>
<DataTemplate x:Key="myTemplate">
<ContentPresenter Content="{Binding}"/>
</DateTemplate>
当我使用它时,我希望 contentpresenter 具有与 GridViewColumn 相同的宽度。到目前为止,我已经尝试使用RelativeSource 绑定,但我似乎根本无法获取GridViewColumn。
如何从我的 DataTemplate
中的 ContentPresenter
获取父级 GridViewColumn
的 ActualWidth
?
I have the following XAML:
<GridView x:Key="myGridView">
<GridViewColumn CellTemplate="{StaticResource myTemplate}"/>
</GridView>
<DataTemplate x:Key="myTemplate">
<ContentPresenter Content="{Binding}"/>
</DateTemplate>
When I use it, I want the contentpresenter to have the same width as the GridViewColumn. So far, I've tried using the RelativeSource binding but I can't seem to get the GridViewColumn at all.
How do I get the ActualWidth
from the parent GridViewColumn
from the ContentPresenter
in my DataTemplate
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GridView 的问题在于,在生成的控件层次结构中没有将单元格作为可视元素的概念。为了使您的
ContentPresenter
与列一样宽,您应该将ListViewItem
的HorizontalContentAlignment
属性设置为Stretch
。这样,ContentPresenter
将拉伸到可用宽度,即列的宽度。下面是一个示例 XAML,它说明了这一点:
The problem with
GridView
is that there is no notion of cells as visual elements in the generated control hierarchy. In order for yourContentPresenter
to be wide as the column you should set theHorizontalContentAlignment
property of theListViewItem
toStretch
. This way theContentPresenter
will stretch to the available width, which is the width of the column.Here is a sample XAML which illustrates this: