.net - WPF - 没有容器的数据模板,项目作为网格中的行?

标签 .net wpf templates xaml grid

是否有与 ItemsControl.ItemTemplateGrid 配合使用的等效机制?我有一个项目集合,我想将它们呈现为 Grid 中的行,以便我可以将 Grid.Column 分配给模板内的各个元素(如与列表控件中的行相反)。在使用标准控件的 WPF 中这可能吗?

最佳答案

好的,使用带有 Grid.IsSharedSizeScope="true" 的 ItemsControl应用附加属性(property)。接下来,对于您的 ItemTemplate,您使用 <Grid>就像您通常所做的那样,除了现在添加 ColumnDefinition 时您设置 SharedSizeGroup属性为每列唯一的名称。例如:

<ItemsControl Grid.IsSharedSizeScope="true">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="MyFirstColumn" />
                    <ColumnDefinition SharedSizeGroup="MySecondColumn" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding MyFirstProperty}"/ >
                <TextBlock Grid.Column="1" Text="{Binding MySecondProperty}"/ >
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

有关 IsSharedSizeScope 和 SharedSizeGroup 的更多信息,check out this section of the SDK 。应该注意的是,RowDefinitions 也有一个 SharedSizeGroup,以便您也可以进行水平布局。

关于.net - WPF - 没有容器的数据模板,项目作为网格中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639948/

相关文章:

c# - MethodImplAttribute(InternalCall, Runtime) 对 COM Interop 接口(interface)的方法有何作用?

c++ - 类型定义非类型模板参数

C++ 元编程 doxygen 文档

wpf - ICollectionView.Refresh 不会重新过滤集合

c++ - 在虚函数上使用 enable_if

c# - 使用 Expression 类构造 LINQ 表达式时按正则表达式搜索

c# - 为什么根据 ReSharper 将 double-to-int 转换分配给冗余的动态变量?

c# - 使用 Linq to Entities 执行字符串搜索

c# - WPF:在工具箱中看不到我的自定义控件

c# - 在显示另一个之前隐藏所有可见的 Metro 对话框