DataGrid中的WPF Visual C#DataGrid

时间:2016-10-04 12:48:03

标签: c# wpf xaml datagrid

正如标题所说,我尝试在DataGrid中获取DataGrid。使用我的代码,只有Columnnames出现。在某些时候,我可以显示来自第一个DataGrid的数据,但现在却没有。

XAML:

<Window x:Class="WpfDataGridSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="400">
    <Grid Margin="10">
        <DataGrid Name="dgUsers" AutoGenerateColumns="False" ItemsSource="{Binding User}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid Name="dgUserAddress" AutoGenerateColumns="False" ItemsSource="{Binding Address}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Street" Binding="{Binding Street}" />
                            <DataGridTextColumn Header="Number" Binding="{Binding Number}" />
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</Window>

XAML.CS:

namespace WpfDataGridSample
{
    public partial class MainWindow: Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<User> User = new List<User>();

            var user1 = new User()
            {
                Id = 1,
                Name = "John Doe",
                Birthday = new DateTime(1971,7,23)
            };

            user1.Address.Add(new Address()
            {
                Street = "Teststreet1",
                Number = 1
            });

            user1.Address.Add(new Address()
            {
                Street = "Teststreet1",
                Number = 11
            });

            User.Add(user1);

            var user2 = new User()
            {
                Id = 2,
                Name = "Jane Doe",
                Birthday = new DateTime(1974,1,17)
            };

            user2.Address.Add(new Address()
            {
                Street = "Teststreet2",
                Number = 2
            });

            user2.Address.Add(new Address()
            {
                Street = "Teststreet2",
                Number = 22
            });

            User.Add(user2);

            var user3 = new User()
            {
                Id = 3,
                Name = "Sammy Doe",
                Birthday = new DateTime(1991,9,2)
            };

            user3.Address.Add(new Address()
            {
                Street = "Teststreet3",
                Number = 3
            });

            user3.Address.Add(new Address()
            {
                Street = "Teststreet3",
                Number = 33
            });

            User.Add(user3);
        }
    }

    public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public DateTime Birthday { get; set; }

        public List<Address> Address { get; set; }

        public User()
        {
            Address = new List<Address>();
        }
    }

    public class Address
    {
        public string Street { get; set; }
        public int Number { get; set; }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

namespace WpfDataGridSample {
 public partial class MainWindow : Window {

    public List<User> User { get; set; }

    public MainWindow() {
        InitializeComponent();

        User = new List<User>();

        var user1 = new User() {
            Id = 1,
            Name = "John Doe",
            Birthday = new DateTime(1971, 7, 23)
        };

        user1.Address.Add(new Address() {
            Street = "Teststreet1",
            Number = 1
        });

        user1.Address.Add(new Address() {
            Street = "Teststreet1",
            Number = 11
        });

        User.Add(user1);

        var user2 = new User() {
            Id = 2,
            Name = "Jane Doe",
            Birthday = new DateTime(1974, 1, 17)
        };

        user2.Address.Add(new Address() {
            Street = "Teststreet2",
            Number = 2
        });

        user2.Address.Add(new Address() {
            Street = "Teststreet2",
            Number = 22
        });

        User.Add(user2);

        var user3 = new User() {
            Id = 3,
            Name = "Sammy Doe",
            Birthday = new DateTime(1991, 9, 2)
        };

        user3.Address.Add(new Address() {
            Street = "Teststreet3",
            Number = 3
        });

        user3.Address.Add(new Address() {
            Street = "Teststreet3",
            Number = 33
        });

        User.Add(user3);

        this.DataContext = this;
    }      
}

public class User {
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime Birthday { get; set; }

    public List<Address> Address { get; set; }

    public User() {
        Address = new List<Address>();
    }
}

public class Address {
    public string Street { get; set; }
    public int Number { get; set; }
}
}

我已经在我的机器上测试过它工作正常......

答案 1 :(得分:0)

List<User> User = new ObservableCollection<User>();

DataContext = this;

将设置数据上下文,并通知视图集合中的更改。