Wpf Datagrid扩展到窗口外

Wpf Datagrid扩展到窗口外,wpf,xaml,datagrid,Wpf,Xaml,Datagrid,大家好。我的窗口中的datagrid有问题。我希望它只扩展到它需要的大小,如果它没有填满整个窗口或显示一个滚动条,如果它确实占据了整个屏幕。这是我的xaml <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto

大家好。我的窗口中的datagrid有问题。我希望它只扩展到它需要的大小,如果它没有填满整个窗口或显示一个滚动条,如果它确实占据了整个屏幕。这是我的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>

点击
如果我将datagrid的行高设置为*它会将datagrid的灰色背景延伸到整行。但是如果我将高度设置为“自动”,那么当窗口中有太多项目时,它将不会显示滚动条


有什么想法吗?

您是否尝试过将DataGrid嵌套在数据库中?

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

<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;
    }
}
公共部分类StackOverflowQuestion:UserControl
{
公共堆栈溢出问题()
{
数据=新的ObservableCollection();
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
添加(新元组(“test”,“test”));
初始化组件();
Root.DataContext=this;
}
公共可观测收集数据{
得到;
设置
}
}
结果是一个正确的滚动条控件:

而在另一个方向:

除非我完全误解了你的问题

更新:添加了全屏快照:


是的,它也不会给出期望的结果。如果RowHeight=*的话,它仍然会以RowHeight=auto的形式从屏幕上延伸出来,并且其行为与datagrid非常相似。如果我将数据网格嵌套在scrollviewer和stackpanel中,那么经过更多的混乱之后,我得到了想要的结果。你能把这句话写在你的回答中给其他人听吗?根据我在下面发布的内容,我甚至不确定你是否需要scrollviewer。尝试没有足够的项目来填充屏幕,告诉我你看到的是datagrid的背景还是窗口的背景。用全屏快照更新。是的,这与我遇到的问题相同。数据网格的灰色背景覆盖了我想要看到的窗口的背景,直到数据网格中有足够的项目填满屏幕。我会将dg的背景设置为透明,但我们的数据网格在边界周围有一个阴影。这在不可见的背景周围创建了一个随机阴影