【问题标题】:Datagrid extends off window数据网格延伸到窗口外
【发布时间】:2011-04-25 18:26:12
【问题描述】:

大家好。我的窗口中的数据网格有问题。如果它没有填满整个窗口,我希望它只扩展到所需的大小,或者如果它确实占据了整个屏幕,则显示滚动条。这是我的xml

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

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

有什么想法吗?

【问题讨论】:

    标签: wpf xaml datagrid


    【解决方案1】:

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

    【讨论】:

    • 是的,它也没有给出想要的结果。它仍然会在 RowHeight=auto 时延伸到屏幕之外,并且如果 RowHeight=* 表现得非常类似于只是一个数据网格
    • 经过一番折腾之后,如果我将数据网格嵌套在滚动查看器和堆栈面板中,就会得到我想要的结果。你能把它写在你给别人的答案中吗?
    • 根据我在下面发布的内容,我什至不确定您是否需要滚动查看器。
    【解决方案2】:

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

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

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

    而在另一个方向:

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

    更新:添加全屏截图:

    【讨论】:

    • 尝试没有足够的项目来填满屏幕并告诉我您是否看到了数据网格的背景或窗口的背景。
    • 更新了全屏截图。
    • 是的,这与我遇到的问题相同。数据网格的灰色背景覆盖了我想看到的窗口背景,直到数据网格中有足够的项目来实际填满屏幕。我会将 dg 的背景设置为透明,但我们的数据网格在边框周围有阴影。这在不可见的背景周围创建了一个随机阴影
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2020-10-03
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多