Reputation: 139
I have a ListView, where the item template is another ListView, which has an item template of a TextBox. The end result of this is that I'm able to bind my collection of "rows" to the first ListView and I get a 2D grid of my data. I'm essentially trying to implement a basic spreadsheet.
My problem is that I want the TextBoxes to stretch horizontally so that they are all the same width and take up all of the available space.
I've removed all extraneous style, event handling, context menu, etc - here is the code as it is:
<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>
I've tried setting HorizontalContentAlignment="Stretch" on the ItemContainerStyle of the ListView, and HorizontalAlignment="Stretch" on the TextBox itself with no luck so far. I did have this "working" by binding the width of the TextBoxes to a dependency property on the control which was updated with an attempt to calculate the correct width for all boxes, but it was rather ugly and slow.
Does anyone know how I can get this done in the XAML alone?
Upvotes: 2
Views: 352
Reputation: 8786
Don't use StackPanel
, use UniformGrid
instead in your ItemsPanelTemplate
and set its Columns
to the number of your desired number of fields in each row.
<ItemsPanelTemplate>
<UniformGrid Columns="5"/>
</ItemsPanelTemplate>
Which will:
Provides a way to arrange content in a grid where all the cells in the grid have the same size.
Upvotes: 1