WPF ScrollViewer围绕DataGrid会影响列宽 [英] WPF ScrollViewer around DataGrid affects column width

查看:302
本文介绍了WPF ScrollViewer围绕DataGrid会影响列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ScrollViewer时遇到问题,该问题用于滚动包含数据网格的用户控件。没有滚动查看器,列将按我的意愿填充数据网格,但是在添加滚动查看器时,列将缩小为〜15px。我能够简化布局,并且仍然可以重现此行为。

I have a problem with a ScrollViewer that I use to scroll a user control that contains a data grid. Without the scroll viewer the columns fill the data grid as I want but when adding a scroll viewer the columns shrink to ~15px. I was able to simplify my layout and can still reproduce this behaviour.

将datagrid宽度绑定到另一个控件时,列具有其正常的显示方式,但是与datagrid上的固定宽度一样,具有令人惊讶的相同效果。
我想我不是第一个遇到这个问题的人。我该如何解决此问题,以使网格将其大小调整为可用空间并为其列指定比例宽度?

When binding the datagrid width to another control the columns have their normal with, but that has unsuprisingly the same effect as a fixed width on the datagrid. I guess I'm not the first one who has this problem. How can I work around this behaviour to have my grid adjusting its size to the available space and give it's columns a propotional width?

使用scrollviewer:

,不包括:

With scrollviewer: and without:

<Window x:Class="GridTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid MinWidth="200">
    <DataGrid Margin="0" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="Column A" Width="*"/>
            <DataGridCheckBoxColumn Header="Column B" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</ScrollViewer>

推荐答案

是的,我前一段时间遇到了这个问题,我采取了一种变通办法,以防万一它有用的时候我会在这里发布

Yeah, I had this problem a while ago, I resorted to a workaround, I will post here just incase its useful

<ScrollViewer HorizontalScrollBarVisibility="Auto">
    <Grid x:Name="grid" MinWidth="200">
        <DataGrid Width="{Binding ElementName=grid, Path=ActualWidth}">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Column A" Width="1*"/>
                <DataGridCheckBoxColumn Header="Column B" Width="1*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</ScrollViewer>

技巧是给 DataGrid 一个宽度,在这种情况下,我将绑定到包含元素

The trick was to give the DataGrid a width, in this case I bound back to the containing element

或者如果您不使用 Horizo​​ntalScrollBar ,可以禁用它,这将允许 ScrollViewer 计算宽度,从而允许DataGrid计算宽度。

Or if you don't use the HorizontalScrollBar you can disable it, this will allow the ScrollViewer to calculate a width allowing the DataGrid to calculate a width.

这篇关于WPF ScrollViewer围绕DataGrid会影响列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆