水平滚动条始终出现在WPF DataGrid中(Horizontal Scroll Bar Always Appearing in WPF DataGrid)

编程入门 行业动态 更新时间:2024-10-26 20:29:27
水平滚动条始终出现在WPF DataGrid中(Horizontal Scroll Bar Always Appearing in WPF DataGrid)

我正面临一个问题。 在我看来,我有一个DataGrid,其水平滚动条的可见性设置为自动。 我面临的问题是,即使有足够的空间让DataGrid扩展并完全显示自己,滚动条也会一直出现。 我已经尝试了几乎所有的东西,但无法弄清楚什么是错的。

我上传了一个示例应用程序来证明这里的问题。 希望有人能指出我正在制造的错误。

I am facing an issue. In my view I have a DataGrid whose Horizontal Scroll Bar's Visibility is set to Auto. Problem I am facing is that the scroll bar is appearing all the time even if there is enough space for DataGrid to expand and completely display itself. I have tried almost everything but couldn't figure out whats is wrong.

I have uploaded a sample application to demonstrate the problem here. Hoping anyone will point out mistake I am making.

最满意答案

我做了以下更改/解决方法来解决问题。 它可能不是完美的解决方案,但对我有用。

我在Grid添加了两个列,一个在开头,另一个在结尾。

<Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding ElementName=m_DataGrid, Path=RowHeaderWidth}" /> <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col2, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col3, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col4, Path=ActualWidth}" /> <ColumnDefinition x:Name="specialCol" /> </Grid.ColumnDefinitions>

然后我增加了DataGrid的ColumnSpan

...Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" ....

然后我在Constrcutor of Window添加了这一行

specialCol.Width = new GridLength (2);

并将UpdateGrid方法更改为此

void UpdateGrid() { ScrollViewer scrollview = FindVisualChild<ScrollViewer>(m_DataGrid); Visibility verticalVisibility = scrollview.ComputedVerticalScrollBarVisibility; if (verticalVisibility == System.Windows.Visibility.Visible) { specialCol.Width = new GridLength(20); m_Border.Width = m_DataGrid.ActualWidth - m_DataGrid.RowHeaderWidth - 17; } else { specialCol.Width = new GridLength(2); m_Border.Width = m_DataGrid.ActualWidth - m_DataGrid.RowHeaderWidth; } }

在需要之前,我现在看不到任何HorizontalScrollBar

I did following changes/workarounds to solve the issue. It might not be the perfect solution but works for me.

I added two more columns in the Grid, one in the start and one at the end.

<Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding ElementName=m_DataGrid, Path=RowHeaderWidth}" /> <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col2, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col3, Path=ActualWidth}" /> <ColumnDefinition Width="{Binding ElementName=col4, Path=ActualWidth}" /> <ColumnDefinition x:Name="specialCol" /> </Grid.ColumnDefinitions>

Then I increased the ColumnSpan of DataGrid

...Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" ....

Then I added this line in Constrcutor of Window

specialCol.Width = new GridLength (2);

and changed UpdateGrid method to this

void UpdateGrid() { ScrollViewer scrollview = FindVisualChild<ScrollViewer>(m_DataGrid); Visibility verticalVisibility = scrollview.ComputedVerticalScrollBarVisibility; if (verticalVisibility == System.Windows.Visibility.Visible) { specialCol.Width = new GridLength(20); m_Border.Width = m_DataGrid.ActualWidth - m_DataGrid.RowHeaderWidth - 17; } else { specialCol.Width = new GridLength(2); m_Border.Width = m_DataGrid.ActualWidth - m_DataGrid.RowHeaderWidth; } }

I don't see any HorizontalScrollBar now until it is required

更多推荐

DataGrid,问题,滚动,电脑培训,计算机培训,IT培训"/> <meta name="description&qu

本文发布于:2023-07-15 05:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现在   滚动条   水平   WPF   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!