Reputation: 3473
I'm trying to create a ItemsControl using a Grid to assure the whole space is taken and splittenbetween the Columns. As example, something like this with DataTemplates:
The Grid-Control was the only one I found, which has this attribute. Since Grids don't work that well with dynamic Cells, I used the answer here, to bind against my Model: WPF Grid as ItemsPanel for a list dynamically bound to an ItemsControl
This works so far, and my Code is looking like this:
<DataTemplate x:Key="DtTaskBoardSection" DataType="{x:Type m:TaskBoardSection}">
<uc:TaskBoardSectionControl
AttachedTaskBoardSection="{Binding}" />
</DataTemplate>
<DataTemplate x:Key="DtTaskBoardSectionList" DataType="{x:Type m:TaskBoardSectionList}">
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource DtTaskBoardSection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid
ShowGridLines="True"
xf:DynamicGridSizeBinding.ColumnCount="{Binding Count}"
Background="LightGreen" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--https://stackoverflow.com/questions/2432847/bind-grid-row-grid-column-inside-a-datatemplate-->
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="Grid.Column" Value="{Binding Sequence}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
Unfortunately, in the real project, it now just stacks the Controls like a StackPanel would:
The light-green background represents the Grid, but I'd hope my two Test-Controls just take 50% of the place each, which isn't the case.
Unfortunately, I have a hard time to dig into the internals of the ItemTemplate-Logic, but it seems like overwriting it causes the Grid not only to overwrite the direct Item-Properties, but also some important ones for the Content-Alignment. I guess I'm missing a part of the Logic here, could you give me a hint, what part of the WPF-Templating procedure I'm missing?
Upvotes: 1
Views: 65
Reputation: 128060
Your DynamicGridSizeBinding.ColumnCount
property is probably creating the ColumnDefinitions with the GridUnitType
of their Widths set to Auto
, while it should be Star
:
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
However, for evenly sized columns you wouldn't need a Grid with an attached ColumnCount
property. Better use a UniformGrid with a single row:
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource DtTaskBoardSection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Upvotes: 3