DataGrid 周围的 WPF ScrollViewer 影响列宽 [英] WPF ScrollViewer around DataGrid affects column width

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

问题描述

我使用 ScrollViewer 来滚动包含数据网格的用户控件时遇到问题.如果没有滚动查看器,列会根据需要填充数据网格,但是当添加滚动查看器时,列会缩小到 ~15px.我能够简化我的布局,并且仍然可以重现这种行为.

当将数据网格宽度绑定到另一个控件时,列有它们的法线,但这与数据网格上的固定宽度具有相同的效果.我想我不是第一个遇到这个问题的人.我该如何解决这种行为,让我的网格将其大小调整为可用空间并为其列提供一个比例宽度?

使用滚动查看器:并且没有:

<ScrollViewer Horizo​​ntalScrollBarVisibility="自动"><Grid MinWidth="200"><DataGrid Margin="0" AutoGenerateColumns="False"><DataGrid.Columns><DataGridCheckBoxColumn Header="A 列" Width="*"/><DataGridCheckBoxColumn Header="B 列" Width="*"/></DataGrid.Columns></DataGrid></网格></ScrollViewer>

解决方案

是的,我前段时间遇到了这个问题,我求助于一个解决方法,我会在这里发布,以防万一它有用

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

诀窍是给 DataGrid 一个宽度,在这种情况下我绑定回包含元素

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

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.

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?

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>

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

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.

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

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