简体   繁体   English

WPF DataGrid不绑定到集合

[英]WPF DataGrid Not Binding to Collection

For some reason my DataGrid which is contained in a WPF UserControl is not binding to a collection. 出于某种原因,WPF UserControl中包含的DataGrid没有绑定到集合。 It always comes out with headers only and none of the rows for the content are added. 它总是只带有标题,而没有添加内容的任何行。

Users.xaml.cs: Users.xaml.cs:

 public partial class Users : UserControl
    {
        private ObservableCollection<UserViewModel> _userViewModels;

        public ObservableCollection<UserViewModel> UserViewModels
        {
            get { return _userViewModels; }
            set { _userViewModels = value;  }
        }

        public Users()
        {
            InitializeComponent();
            PopulateUsers(); 
        }      

        private void PopulateUsers()
        {
            _userViewModels = new ObservableCollection<UserViewModel>() 
            {
                new UserViewModel() { FirstName = "Mary", LastName = "Doe"}, 
                new UserViewModel() { FirstName = "John", LastName = "Doe"}
            };

            this.DataContext = _userViewModels; 
        }


    }

Users.xaml: Users.xaml:

<UserControl x:Class="Users.Views.Users"
             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">

   <DataGrid ItemsSource="{Binding Source=UserViewModels}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>

            <DataGridTextColumn Header="Last Name"></DataGridTextColumn>           
        </DataGrid.Columns>
    </DataGrid>

</UserControl>

Can anyone notice anything I am missing? 有人可以注意到我所缺少的吗?

UPDATE 1: 更新1:

I updated the XAML code to the following: 我将XAML代码更新为以下内容:

<DataGrid ItemsSource="{Binding Source=UserViewModels}" AutoGenerateColumns="False">

Now, I can see the DataGrid but it does not have any values. 现在,我可以看到DataGrid,但它没有任何值。 It is all empty! 都是空的! Even though I am populating two fields "FirstName" and "LastName". 即使我填充了两个字段“ FirstName”和“ LastName”。

You are doing it wrong. 你做错了。 Also, binding is more useful if you have ViewModel . 另外,如果您具有ViewModel ,则绑定更有用。

The reason why it didn't work the first time is because you were trying to set the entire view as the DataContext . 第一次不起作用的原因是因为您试图将整个视图设置为DataContext Therefore, the binding was trying to look for a property of UserViewModels inside the UserViewModels . 因此,绑定试图在UserViewModels内部寻找UserViewModelsUserViewModels

Change 更改

this.DataContext = _userViewModels; 

to

this.DataContext = this; 

and put the bindings back what it originally is 并把绑定放回原来的样子

ItemsSource="{Binding UserViewModels}" 

Change ItemsSource binding to ItemsSource绑定更改为

ItemsSource="{Binding}"

DataContext , for whole UserControl , is already set to your collection when you do 整个UserControl DataContext在执行时已设置为您的集合

this.DataContext = _userViewModels

so all you need to do is bind ItemsSource to current DataContext 所以您要做的就是将ItemsSource绑定到当前的DataContext

You need to update your bindings and set the DataContext of the UserControl. 您需要更新绑定并设置UserControl的DataContext。 This can be done strictly in xaml as shown below. 可以严格在xaml中完成此操作,如下所示。

xaml: XAML:

<UserControl x:Class="Users.Views.Users"
             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"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
   <DataGrid ItemsSource="{Binding UserViewModels}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>        
        </DataGrid.Columns>
    </DataGrid>
</UserControl>

and remove this line: 并删除此行:

this.DataContext = _userViewModels; 

The above code will set the current DataContext of the UserControl to itself. 上面的代码会将UserControl的当前DataContext设置为其自身。 When this is applied, you are able to bind accordingly, as shown above. 应用此功能后,您可以进行相应的绑定,如上所示。 Note that your XAML did not have anything binding to your Last Name column, so I added that as well. 请注意,您的XAML没有绑定到“姓氏”列,因此我也添加了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM