Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make WPF datagrid columns autoresize from the beginning so they are all visible?

Tags:

c#

wpf

datagrid

I have a datagrid that has a column (the last one) that should always show when there is room for it. Unfortunately I'm not able to make this happen and can't find a solution anywhere.

this WPF application is available in portrait mode and landscape mode...

When starting the application in portrait mode, I want the datagrid to always show the last column, but initially it hides several columns. But when resizing the application (getting a larger width) by going to landscape, and then back to portrait mode, it does work as expected.

Most columns have their width set to either Auto or *. Only the last column has a minimum width of 80 pixels Because I don't want that one to resize, It should always show.

Here are the settings of the grid:

            <DataGrid Grid.Row="1"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  HorizontalScrollBarVisibility="Disabled"
                  ItemsSource="{Binding Path=MyCollection,
                                        Mode=OneWay}"
                  RowDetailsVisibilityMode="Collapsed"
                  RowHeaderStyle="{StaticResource ExpanderRowHeaderStyle}"
                  SelectionMode="Single"
                  SelectionUnit="Cell">

Here is the initial situation in portrait mode Initial image

Then when I resize to landscape:

landscape mode

And finally when I go back to portrait mode, I get the desired result: enter image description here

What can I change in order to have my initial situation look like picture 3... So that all columns are shown from the beginning (without the need to resize)?

EDIT: For clarity: when I say portrait and landscape mode, I mean that the software is used on windows tablets, but the same problem occurs on regular screens aswell. When the window is initially small, not all columns show, but when I enlarge the window and then resize back to the initial small size, all columns do show.

EDIT 2:

I can fix the problem if I change the width of the first column to * instead of Auto.... I don't know why though....... Here is the Full DataGrid

                        <DataGrid Grid.Row="1"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  HorizontalScrollBarVisibility="Disabled"
                  ItemsSource="{Binding Path=BstCollection,
                                        Mode=OneWay}"
                  RowDetailsVisibilityMode="Collapsed"
                  RowHeaderStyle="{StaticResource ExpanderRowHeaderStyle}"
                  SelectionMode="Single"
                  SelectionUnit="Cell"
                  EnableRowVirtualization="False" Loaded="DataGrid_Loaded" DataContextChanged="DataGrid_DataContextChanged" ColumnWidth="*">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource IsLastOrderToBooleanMultiConverter}">
                                    <MultiBinding.Bindings>
                                        <Binding Mode="OneWay"
                                                 Path="BstCollection[0].BesbstldOp"
                                                 Source="{StaticResource BstGbrModel}" />
                                        <!--  ReSharper disable Xaml.BindingWithContextNotResolved  -->
                                        <Binding Mode="OneWay" Path="BesbstldOp" />
                                        <Binding Mode="OneWay" Path="BesHoeveelheid" />
                                        <!--  ReSharper restore Xaml.BindingWithContextNotResolved  -->
                                    </MultiBinding.Bindings>
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Background" Value="PowderBlue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="Auto" Header="{specialLocalization:Translate CtlBstGbr--grdBsten--atlOmschrijving}" IsReadOnly="True">
                    <DataGridTemplateColumn.CellEditingTemplate >
                        <DataTemplate DataType="bl:Bst">
                            <TextBlock Text="{Binding atlOmschrijving, Mode=OneWay}" IsEnabled="False" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate DataType="bl:Bst">
                            <TextBlock >
                                <Run Text="{Binding atlOmschrijving, Mode=OneWay}"/>
                                <Run Foreground="Red" Text="{Binding Path=atl.atlVKMat, Mode=OneWay, Converter={StaticResource BooleanToBstTextConverter}}" />
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>
                <DataGridTextColumn Width="Auto"
                                    Binding="{Binding Path=BesbstldOp,
                                                      Mode=OneWay,
                                                      Converter={StaticResource DateToStringValueConverter}}"
                                    Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesbstldOp}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Width="Auto"
                                    Binding="{Binding Path=BesGeleverdOp,
                                                      Mode=OneWay,
                                                      Converter={StaticResource DateToStringValueConverter}}"
                                    Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesGeleverdOp}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Width="*"
                                    Binding="{Binding Path=BesHoeveelheid,
                                                      Mode=OneWay,
                                                      Converter={StaticResource IntToStringValueConverter}}"
                                    Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesHoeveelheid}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Width="Auto"
                                    Binding="{Binding Path=atlbstleenheid,
                                                      Mode=OneWay}"
                                    Header="{specialLocalization:Translate CtlBstGbr--grdBsten--atlbstleenheid}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Width="*"
                                    Binding="{Binding Path=vpeNaam,
                                                      Mode=OneWay}"
                                    Header="{specialLocalization:Translate CtlBstGbr--grdBsten--vpeNaam}"
                                    IsReadOnly="True" />
                <DataGridTemplateColumn Width="*" MinWidth="85" Header="{specialLocalization:Translate CtlBstGbr--grdBsten--NHoevheid}" >
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate DataType="bl:Bst">
                            <TextBlock Text="{Binding NHoevheid, Mode=OneWay}" IsEnabled="{Binding atl.atlVKMat, Mode=OneWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate DataType="bl:Bst">
                            <customControls:UpDownTextBox MinValue="0" Text="{Binding NHoevheid, Mode=TwoWay}"  Visibility="{Binding Path=atl.atlVKMat, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>


            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate DataType="bl:Bst">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0"
                               Grid.Column="0"
                               Content="{specialLocalization:Translate CtlBstGbr--lblatl--}" />
                        <TextBox Grid.Row="0"
                                 Grid.Column="1"
                                 IsReadOnly="True"
                                 Text="{Binding Path=atlOmschrijving,
                                                Mode=OneWay}" />
                        <Label Grid.Row="1"
                               Grid.Column="0"
                               Content="{specialLocalization:Translate CtlBstGbr--lblInfo--}" />
                        <TextBox Grid.Row="1"
                                 Grid.Column="1"
                                 Height="75"
                                 AcceptsReturn="True"
                                 IsReadOnly="True"
                                 Text="{Binding Path=atl.ArtInfo,
                                                Mode=OneWay}"
                                 TextWrapping="WrapWithOverflow"
                                 VerticalScrollBarVisibility="Visible" />
                    </Grid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
like image 509
user1841243 Avatar asked Feb 02 '18 14:02

user1841243


1 Answers

Make sure the first column has a width of "*". A width of "Auto", will make the width of the first column initialize to the full width of the content.

like image 196
user1841243 Avatar answered Sep 30 '22 18:09

user1841243