Datagrid在窗口外延伸

时间:2011-04-25 18:26:12

标签: wpf xaml 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的行高设置为*,它会将数据网格的灰色背景向下延伸到整行。但是,如果我将高度设置为自动,那么当窗口的项目太多时,它将不会显示滚动条。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试将DataGrid嵌套在ScrollViewer

答案 1 :(得分:0)

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

<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;
    }
}

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

enter image description here

另一方面:

enter image description here

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

更新:添加全屏截图:

enter image description here