0

I have a problem with a StackPanel and its visibility in a DatagridCellTemplateColumn.

My ControlTemplate looks like this:

<ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate">
    <StackPanel Name="CellGrid" Orientation="Horizontal" Tag="{TemplateBinding Tag}"
         Visibility="{Binding RelativeSource={RelativeSource Self},
             Path=Tag.IsNotEmpty, Converter={StaticResource BoolToVisibility}}">

        <CheckBox Margin="4,1,2,1"
            IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <xceed:IntegerUpDown Margin="2,1" Maximum="99" Minimum="1"
            Value="{Binding Path=Tag.Priority, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Button Margin="2,1,4,1" Command="{Binding Path=Tag.ShowDetailCommand, ElementName=CellGrid}"
            Content="{Binding Path=Tag.ItemsCountString, ElementName=CellGrid}"
            ContentStringFormat="{}{0:}" />
    </StackPanel>
</ControlTemplate>

Usage of this template looks like this:

 <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
         <Control Width="auto" Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
     </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>

I want to make a DataGrid where the width of all columns is set to the width of the current content. But when the content of a column is collapsed, then the width of a column should be given only by the width of the header. Actually I don't know what is the problem in my solution, but all my columns have the same width, even if their content is collapsed.

What I'm trying to achieve looks like this: wanted result

What I'm getting looks like this: current result

And this is code of the DataGrid

       <DataGrid Grid.Row="0"
              AutoGenerateColumns="False"
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              CanUserReorderColumns="False"
              CanUserResizeColumns="False"
              CanUserResizeRows="False"
              ItemsSource="{Binding JobBomPriorityRows,
                                    UpdateSourceTrigger=PropertyChanged}">
        <!--    -->
        <DataGrid.Resources>
            <vc:BoolToVisibilityConverter x:Key="BoolToVisibility" />
            <ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate" TargetType="Control">
                <StackPanel Name="CellGrid"
                            Width="auto"
                            Orientation="Horizontal"
                            Tag="{TemplateBinding Tag}"
                            Visibility="{Binding RelativeSource={RelativeSource Self},
                                                 Path=Tag.IsNotEmpty,
                                                 Converter={StaticResource BoolToVisibility}}">

                    <CheckBox Margin="4,1,2,1" IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <xceed:IntegerUpDown Margin="2,1"
                                         Maximum="99"
                                         Minimum="1"
                                         Value="{Binding Path=Tag.Priority,
                                                         ElementName=CellGrid,
                                                         Mode=TwoWay,
                                                         UpdateSourceTrigger=PropertyChanged}" />
                    <Button Margin="2,1,4,1"
                            Command="{Binding Path=Tag.ShowDetailCommand,
                                              ElementName=CellGrid}"
                            Content="{Binding Path=Tag.ItemsCountString,
                                              ElementName=CellGrid}"
                            ContentStringFormat="{}{0:}"
                            Padding="3,0" />
                </StackPanel>
            </ControlTemplate>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Width="*"
                                Binding="{Binding Week}"
                                Header="Week"
                                IsReadOnly="True"
                                SortDirection="Ascending" />
            <DataGridTextColumn Width="*"
                                Binding="{Binding JobBom}"
                                Header="Job Bom"
                                IsReadOnly="True" />
            <DataGridTemplateColumn Width="auto"
                                    Header="Web"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="Flange"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding Flanges}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="Plate"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding Plates}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="Gusset"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding Gussets}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="SP"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="STD G"
                                    IsReadOnly="True">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding STD_Gs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto"
                                    Header="STD SP"
                                    IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Control Tag="{Binding STD_SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
Josef
  • 43
  • 7
  • DataGrid doesnt automatically updates column's width once the content of the cell is loaded. In order to do this, you should set `AutoSizeMode`, as explained [here](http://stackoverflow.com/questions/18666582/datagridview-autofit-and-fill). Did you tried that already? – ltiveron Nov 16 '16 at 11:42
  • Could you share the xaml for the DataGrid, please? – Julien Poulin Nov 16 '16 at 12:15
  • @ltiveron - I can't use property AutoSizeMode because my problem is with control DataGrid and AutoSizeMode shold be at DataGridView – Josef Nov 18 '16 at 07:49
  • You are right, sorry about the confusion. Have you tried [this](http://stackoverflow.com/questions/4577944/how-to-resize-wpf-datagrid-to-fit-its-content) ? – ltiveron Nov 18 '16 at 09:48

0 Answers0