Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮不可见,因为DataGrid_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# 按钮不可见,因为DataGrid

C# 按钮不可见,因为DataGrid,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有以下xaml代码: <Window x:Class="DataGridIssue.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:DataGridIssue"

我有以下xaml代码:

<Window x:Class="DataGridIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:DataGridIssue"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <vm:ViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Grid>
        <DockPanel VerticalAlignment="Top">
            <Button DockPanel.Dock="Top" Content="Test button 1" HorizontalAlignment="Left"/>
            <DataGrid DockPanel.Dock="Top" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/>
            <Button DockPanel.Dock="Top" HorizontalAlignment="Left" Content="Test button 2"/>
        </DockPanel>
    </Grid>
</Window>

和C#视图模型代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace DataGridIssue
{
    public class ViewModel
    {
        public ObservableCollection<Test> TestList { get; set; }

        public ViewModel()
        {
            TestList = new ObservableCollection<Test>();

            for (int i = 1; i <= 30; i++)
            {
                var item = new Test();
                item.TestString = "Element " + i.ToString();
                TestList.Add(item);
            }
        }
    }

    public class Test
    {
        public string TestString { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Collections.ObjectModel;
命名空间DataGridIssue
{
公共类视图模型
{
公共ObservableCollection测试列表{get;set;}
公共视图模型()
{
TestList=新的ObservableCollection();

对于(int i=1;i您应该使用三行的
Grid

...
 <Window.Resources>
        <vm:ViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Content="Test button 1" HorizontalAlignment="Left"/>
        <DataGrid Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/>
        <Button Grid.Row="2" HorizontalAlignment="Left" Content="Test button 2"/>
    </Grid>
</Window>
。。。

我想避免使用高度。@DmitryMofeev您可以使用
自动
自动调整行高度。再次检查我的答案:)是的,这很有用!谢谢!
...
 <Window.Resources>
        <vm:ViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Content="Test button 1" HorizontalAlignment="Left"/>
        <DataGrid Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/>
        <Button Grid.Row="2" HorizontalAlignment="Left" Content="Test button 2"/>
    </Grid>
</Window>