VerticalScrollBarVisibility property of DataGrid control in WPF is invalid

Posted by pullaratt on Sat, 04 Jan 2020 22:45:30 +0100

If the DataGrid control in WPF is placed in the < StackPanel > < StackPanel > panel, VerticalScrollBarVisibility = "Visible" or VerticalScrollBarVisibility = "Auto" is invalid. As follows:

                     <materialDesign:Card Grid.Column="2" Margin="5">
                            <StackPanel Orientation="Vertical"> <!--StackPanel Label>
                                <DataGrid   AutoGenerateColumns="False" 
                                            ItemsSource="{Binding VMs}" 
                                            DataContext="{Binding DataVM}"   
                                            VerticalScrollBarVisibility="Visible">
                                    <DataGrid.ColumnHeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGrid.Columns>
                                        <DataGridTemplateColumn Width="50" Header="evaluate">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <RadioButton IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>

                                        <DataGridTextColumn Width="60" Header="Scale"  Binding="{Binding Index,Mode=OneWay}">
                                        </DataGridTextColumn>
                                  
                                    </DataGrid.Columns>
                                </DataGrid>
                            </StackPanel>
                        </materialDesign:Card>

Then when I delete the StackPanel tag, it will display normal.

                       <materialDesign:Card Grid.Column="2" Margin="5">
                           <!--delete StackPanel>
                                <DataGrid   AutoGenerateColumns="False" 
                                            ItemsSource="{Binding VMs}" 
                                            DataContext="{Binding DataVM}"   
                                            VerticalScrollBarVisibility="Visible">
                                    <DataGrid.ColumnHeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGrid.Columns>
                                        <DataGridTemplateColumn Width="50" Header="evaluate">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <RadioButton IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>

                                        <DataGridTextColumn Width="60" Header="Scale"  Binding="{Binding Index,Mode=OneWay}">
                                        </DataGridTextColumn>
                                  
                                    </DataGrid.Columns>
                                </DataGrid>

                        </materialDesign:Card>

Add: of course, in theory, it doesn't make sense to put a DataGrid in a StackPanel. It shouldn't be put in any way. Sometimes it's not put in. The latter has other elements in the StackPanel and this happens when other elements are deleted. However, when there are other elements in a StackPanel besides a DataGrid, you need to be careful how to display them.