如何让WPF datagrid列从头开始自动调整,以便它们全部可见?

时间:2018-02-02 14:08:49

标签: c# wpf datagrid

我有一个数据网格,它有一个列(最后一个),当它有空间时应始终显示。不幸的是,我无法实现这一点,无法在任何地方找到解决方案。

此WPF应用程序可在纵向模式和横向模式下使用...

在纵向模式下启动应用程序时,我希望datagrid始终显示最后一列,但最初它隐藏了几列。 但是,当通过前景调整应用程序(获得更大的宽度),然后返回纵向模式时,它会按预期工作。

大多数列的宽度设置为Auto或*。只有最后一列的最小宽度为80像素因为我不想让它调整大小,所以应该始终显示。

以下是网格的设置:

            <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">

以下是纵向模式的初始情况 Initial image

然后当我调整景观时:

landscape mode

最后当我回到肖像模式时,我得到了理想的结果: enter image description here

为了让我的初始情况看起来像图3,我可以改变什么?所以所有列都从头开始显示(无需调整大小)?

编辑:为了清楚起见:当我说纵向和横向模式时,我的意思是该软件用于Windows平板电脑,但同样的问题也出现在常规屏幕上。当窗口最初很小时,并非所有列都显示,但是当我放大窗口然后调整回原始的小尺寸时,所有列都会显示。

编辑2:

我可以解决问题,如果我将第一列的宽度更改为*而不是Auto ....我不知道为什么虽然.......这里是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>

1 个答案:

答案 0 :(得分:6)

确保第一列的宽度为“*”。 宽度为“Auto”,将使第一列的宽度初始化为内容的整个宽度。