【问题标题】:Stretch TextBoxes inside ListView uniformly统一拉伸 ListView 内的 TextBox
【发布时间】:2015-03-05 13:23:55
【问题描述】:

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

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

我已经删除了所有无关的样式、事件处理、上下文菜单等 - 下面是代码:

    <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 中完成这项工作?

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

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

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

    哪个会:

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

    【讨论】:

    • 谢谢!看起来通过其他一些小调整,我将获得我所追求的行为。
    猜你喜欢
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2018-08-02
    • 2019-12-26
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多