开发者

Datagrid extends off window

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"/>开发者_JAVA百科
    </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?


Have you tried nesting the DataGrid in a ScrollViewer?


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:

Datagrid extends off window

And in the other direction:

Datagrid extends off window

Unless I'm completely misunderstanding your question?

UPDATE: Added full screen shot:

Datagrid extends off window

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜