数据网格延伸到窗口之外

发布于 2024-11-03 09:54:08 字数 788 浏览 0 评论 0原文

嘿大家。我的窗口中的数据网格有问题。我希望它仅在没有填充整个窗口时扩展到所需的大小,或者在它占据整个屏幕时显示滚动条。这是我的 xaml

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" Name="dg">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Col1"/>
            <DataGridTextColumn Header="Col1"/>
        </DataGrid.Columns>            
    </DataGrid>

    <GroupBox Grid.Row="2" Margin="5">
        <Button>Click</Button>
    </GroupBox>
</Grid>

如果我将数据网格的行高设置为 * ,它会将数据网格的灰色背景向下延伸到整行。但如果我将高度设置为自动,那么当窗口的项目太多时,它不会显示滚动条。

有什么想法吗?

Hey everyone. I have a problem with a datagrid in my window. I want it to extend only to its needed size if it doesn't fill the entire window or show a scroll bar if it does take up the entire screen. Here's my xaml

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" Name="dg">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Col1"/>
            <DataGridTextColumn Header="Col1"/>
        </DataGrid.Columns>            
    </DataGrid>

    <GroupBox Grid.Row="2" Margin="5">
        <Button>Click</Button>
    </GroupBox>
</Grid>

If I set the row height for the datagrid to * it extends the datagrid's gray background down the entire row. But if I set the height to auto, then it won't show a scrollbar when there are too many items for the window.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-11-10 09:54:08

您是否尝试过将 DataGrid 嵌套在 ScrollViewer 中?

Have you tried nesting the DataGrid in a ScrollViewer?

清醇 2024-11-10 09:54:08

我认为问题可能出在其他地方。我在本地尝试了您的代码并创建了一些数据。见下文:

<UserControl x:Class="ControlSandbox.StackOverflowQuestion"
         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 x:Name="Root">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" Name="dg" ItemsSource="{Binding Data}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Col1" Binding="{Binding Item1}" />
            <DataGridTextColumn Header="Col1" Binding="{Binding Item2}"/>
        </DataGrid.Columns>

    </DataGrid>

    <GroupBox Grid.Row="2" Margin="5">
        <Button>Click</Button>
    </GroupBox>
</Grid>

我创建了一些数据,如下所示:

public partial class StackOverflowQuestion : UserControl
{
    public StackOverflowQuestion()
    {
        Data = new ObservableCollection<Tuple<string, string>>();
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));

        InitializeComponent();

        Root.DataContext = this;


    }

    public ObservableCollection<Tuple<String, String>> Data {
        get;
        set;
    }
}

结果是一个正确的滚动条控件:

在此处输入图像描述

以及在另一个方向:

在此处输入图像描述

除非我完全误解了您的问题?

更新:添加了全屏截图:

在此处输入图像描述

I think the problem might be elsewhere. I tried your code locally and created some data. See below:

<UserControl x:Class="ControlSandbox.StackOverflowQuestion"
         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 x:Name="Root">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" Name="dg" ItemsSource="{Binding Data}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Col1" Binding="{Binding Item1}" />
            <DataGridTextColumn Header="Col1" Binding="{Binding Item2}"/>
        </DataGrid.Columns>

    </DataGrid>

    <GroupBox Grid.Row="2" Margin="5">
        <Button>Click</Button>
    </GroupBox>
</Grid>

I created some data like so:

public partial class StackOverflowQuestion : UserControl
{
    public StackOverflowQuestion()
    {
        Data = new ObservableCollection<Tuple<string, string>>();
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));
        Data.Add(new Tuple<string, string>("test", "test"));

        InitializeComponent();

        Root.DataContext = this;


    }

    public ObservableCollection<Tuple<String, String>> Data {
        get;
        set;
    }
}

The result is a properly scrollbar-ed control:

enter image description here

And in the other direction:

enter image description here

Unless I'm completely misunderstanding your question?

UPDATE: Added full screen shot:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文