如何将通过模板创建的单独网格沿其列/行对齐?
我认为在这种情况下一张图片胜过一千个单词:
XAML:
<Grid>
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Background="LightBlue"/>
<TextBlock Text="{Binding Age}" Background="LightPink" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
ps - 我不想为第一列设置一个特定的值,但要为其提供所需的最大值。
更新: 我已经尝试过 ColinE 的链接并完成了此操作:
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
但它对我不起作用。
I thinks that in this case A picture is worth a thousand words:
XAML:
<Grid>
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Background="LightBlue"/>
<TextBlock Text="{Binding Age}" Background="LightPink" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
p.s - I don't want to set a specific with to the first column, but to give it the max with that it needs.
Update:
I've tried ColinE's link and done this:
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
but it didn't worked for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为每一列使用 SharedSizeGroup。
查看本教程...
http://blogs.interknowlogy。 com/johnbowen/archive/2007/08/27/21132.aspx
另外,请确保您的网格的共享大小范围属性为 true:
You need to use a SharedSizeGroup for each column.
Check out this tutorial ...
http://blogs.interknowlogy.com/johnbowen/archive/2007/08/27/21132.aspx
Also, ensure that the shared size scope property is true for your grid:
不使用 Grid,使用 DataGrid 怎么样?
Instead of using Grid how about using DataGrid?