1

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: Simple Grid-Test

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:

Fail

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?

Community
  • 1
  • 1
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • 2
    Please show how you create the ColumnDefinitions. Seems that you set the GridUnitType of their Width to Auto instead of Star. – Clemens Sep 30 '16 at 19:31
  • 2
    Did you look into `UniformGrid`? From what I can see it should work here with just setting `Rows=1`. No need for weird `Grid` extensions. – ghord Sep 30 '16 at 20:51

1 Answers1

3

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>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Uff, that's a double takedown: You are right, I miss-used a property of the Extension, which caused the Width to Auto. Even Further, UniformGrid works also. Interesting enough, I though I used UniformGrid. However, many thanks! – Matthias Müller Oct 01 '16 at 11:02