如何获取 GridViewColumn 的实际宽度

发布于 2024-08-19 07:28:23 字数 529 浏览 5 评论 0原文

我有以下 XAML:

<GridView x:Key="myGridView">
    <GridViewColumn CellTemplate="{StaticResource myTemplate}"/>
</GridView>
<DataTemplate x:Key="myTemplate">
    <ContentPresenter Content="{Binding}"/>
</DateTemplate>

当我使用它时,我希望 contentpresenter 具有与 GridViewColumn 相同的宽度。到目前为止,我已经尝试使用RelativeSource 绑定,但我似乎根本无法获取GridViewColumn。

如何从我的 DataTemplate 中的 ContentPresenter 获取父级 GridViewColumnActualWidth

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-08-26 07:28:23

GridView 的问题在于,在生成的控件层次结构中没有将单元格作为可视元素的概念。为了使您的 ContentPresenter 与列一样宽,您应该将 ListViewItemHorizo​​ntalContentAlignment 属性设置为 Stretch 。这样,ContentPresenter 将拉伸到可用宽度,即列的宽度。

下面是一个示例 XAML,它说明了这一点:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <Grid.Resources>
         <DataTemplate x:Key="leftTemplate">
            <ContentPresenter HorizontalAlignment="Left" Content="{Binding}"/>
         </DataTemplate>
         <DataTemplate x:Key="rightTemplate">
            <ContentPresenter HorizontalAlignment="Right" Content="{Binding}"/>
         </DataTemplate>
         <GridView x:Key="myGridView">
            <GridViewColumn Width="150" CellTemplate="{StaticResource rightTemplate}" Header="Right"/>
            <GridViewColumn Width="150" CellTemplate="{StaticResource leftTemplate}" Header="Left"/>
         </GridView>
         <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
         </Style>
      </Grid.Resources>
      <ListView Margin="10" View="{StaticResource myGridView}">
         <ListViewItem Content="item 1"/>
         <ListViewItem Content="item 2"/>
         <ListViewItem Content="item 3"/>
         <ListViewItem Content="item 4"/>
         <ListViewItem Content="item 5"/>
      </ListView>
   </Grid>
</Page>

The problem with GridView is that there is no notion of cells as visual elements in the generated control hierarchy. In order for your ContentPresenter to be wide as the column you should set the HorizontalContentAlignment property of the ListViewItem to Stretch. This way the ContentPresenter will stretch to the available width, which is the width of the column.

Here is a sample XAML which illustrates this:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <Grid.Resources>
         <DataTemplate x:Key="leftTemplate">
            <ContentPresenter HorizontalAlignment="Left" Content="{Binding}"/>
         </DataTemplate>
         <DataTemplate x:Key="rightTemplate">
            <ContentPresenter HorizontalAlignment="Right" Content="{Binding}"/>
         </DataTemplate>
         <GridView x:Key="myGridView">
            <GridViewColumn Width="150" CellTemplate="{StaticResource rightTemplate}" Header="Right"/>
            <GridViewColumn Width="150" CellTemplate="{StaticResource leftTemplate}" Header="Left"/>
         </GridView>
         <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
         </Style>
      </Grid.Resources>
      <ListView Margin="10" View="{StaticResource myGridView}">
         <ListViewItem Content="item 1"/>
         <ListViewItem Content="item 2"/>
         <ListViewItem Content="item 3"/>
         <ListViewItem Content="item 4"/>
         <ListViewItem Content="item 5"/>
      </ListView>
   </Grid>
</Page>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文