WPF:在 ScrollViewer 中调整数据网格的宽度 [英] WPF: Resize width of datagrid inside ScrollViewer

查看:25
本文介绍了WPF:在 ScrollViewer 中调整数据网格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ScrollViewer 显示页面内容的地方.在里面我有一个 DataGrid,但是当我将 DataGrid 放在 ScrollViewer 中时,列的宽度丢失了.所以我在这里阅读了 http://stackoverflow.com/questions/17875765/wpf-scrollviewer-around-datagrid-affects-column-width我需要将父级的宽度绑定到我的 DataGrid 上,这没问题,但有什么问题.当窗口宽度增加时,DataGrid 的宽度也会增加,但是当窗口宽度减小时,DataGrid 的宽度不会改变.我想要的是当 DataGrid 的宽度更改宽度也更改时.

I have ScrollViewer where is displayed page content. Inside I have a DataGrid, but when I put DataGrid inside ScrollViewer width of columns is lost. So I readed here http://stackoverflow.com/questions/17875765/wpf-scrollviewer-around-datagrid-affects-column-width that I need to bind width of parent to my DataGrid and it's ok but what's the problem. When width of window is increase width of DataGrid is increased too but when width of window is decreased width of DataGrid doesn't change. What I would like to have is when width of is change width of DataGrid is changed too.

这是示例 XAML:

<Window x:Class="WpfApplication1.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 VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <!--Page Content-->
        <Grid x:Name="grid">
            <DataGrid Width="{Binding ElementName=grid, Path=ActualWidth}">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="*" Header="Header 1"  />
                    <DataGridTextColumn Width="*" Header="Header 2" /> 
                    <DataGridTextColumn Width="*" Header="Header 3" /> 
                    <DataGridTextColumn Width="*" Header="Header 4" />
                    <DataGridTextColumn Width="*" Header="Header 5" />
                    <DataGridTextColumn Width="*" Header="Header 6" />
                    <DataGridTextColumn Width="*" Header="Header 7" /> 
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </ScrollViewer>
</Window>

现在我的 MainWindow 看起来像:

EDITED: Now my MainWindow looks like:

<Window x:Class="WpfApplication1.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"
        xmlns:my="clr-namespace:WpfApplication1"
        x:Name="window1">
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <my:MyGrid />
    </ScrollViewer>
</Window>

和我的控制:

<UserControl x:Class="WpfApplication1.MyGrid"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <DataGrid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ActualWidth}">
            <DataGrid.Columns>
                <DataGridTextColumn Width="*" Header="Header 1"  />
                <DataGridTextColumn Width="*" Header="Header 2" />
                <DataGridTextColumn Width="*" Header="Header 3" />
                <DataGridTextColumn Width="*" Header="Header 4" />
                <DataGridTextColumn Width="*" Header="Header 5" />
                <DataGridTextColumn Width="*" Header="Header 6" />
                <DataGridTextColumn Width="*" Header="Header 7" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

推荐答案

试试这个方法:

<Window x:Class="WpfApplication1.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"
        x:Name="window1">
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <!--Page Content-->
            <DataGrid Width="{Binding ElementName=window1, Path=ActualWidth}">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="*" Header="Header 1"  />
                    <DataGridTextColumn Width="*" Header="Header 2" /> 
                    <DataGridTextColumn Width="*" Header="Header 3" /> 
                    <DataGridTextColumn Width="*" Header="Header 4" />
                    <DataGridTextColumn Width="*" Header="Header 5" />
                    <DataGridTextColumn Width="*" Header="Header 6" />
                    <DataGridTextColumn Width="*" Header="Header 7" /> 
                </DataGrid.Columns>
            </DataGrid>
    </ScrollViewer>
</Window>

这个更适合你吗?

这篇关于WPF:在 ScrollViewer 中调整数据网格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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