繁体   English   中英

均匀地在ListView中拉伸文本框

[英]Stretch TextBoxes inside ListView uniformly

我有一个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上的Horizo​​ntalContentAlignment =“ Stretch”,并在TextBox本身上设置Horizo​​ntalAlignment =“ Stretch”,到目前为止还没有运气。 我确实通过将TextBoxes的宽度绑定到控件上的依赖项属性来进行这种“工作”,该属性已更新,试图为所有框计算正确的宽度,但是它相当难看且缓慢。

有谁知道我该如何单独在XAML中完成这项工作?

不要使用StackPanel ,使用UniformGrid在你的,而不是ItemsPanelTemplate并设置其Columns到你想要的每行中的字段数数。

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

这将:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM