首页 文章

在ListView中统一拉伸TextBox

提问于
浏览
1

我有一个ListView,其中项目模板是另一个ListView,它有一个TextBox的项目模板 . 最终的结果是我能够将我的“行”集合绑定到第一个ListView,并获得我的数据的2D网格 . 我基本上是在尝试实现一个基本的电子表格 .

我的问题是我希望TextBoxes水平拉伸,以便它们都具有相同的宽度并占用所有可用空间 .

我删除了所有无关的样式,事件处理,上下文菜单等 - 这里是代码:

<ListView ItemsSource="{Binding Rows}">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding Path=RowData}">
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Value, Mode=TwoWay}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我已经尝试在ListView的ItemContainerStyle上设置HorizontalContentAlignment =“Stretch”,并且在TextBox本身上设置HorizontalAlignment =“Stretch”,到目前为止没有运气 . 我确实通过将TextBoxes的宽度绑定到控件上的依赖属性来实现这个“工作”,该控件已更新,尝试计算所有框的正确宽度,但它相当丑陋和缓慢 .

有谁知道我怎么能单独在XAML中完成这项工作?

1 回答

  • 1

    不要使用 StackPanel ,而是在 ItemsPanelTemplate 中使用UniformGrid并将其 Columns 设置为每行所需字段数 .

    <ItemsPanelTemplate>
       <UniformGrid Columns="5"/>
    </ItemsPanelTemplate>
    

    哪个会:

    提供一种在网格中排列内容的方法,其中网格中的所有单元格具有相同的大小 .

相关问题