Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Datagrid bounded to Observable Collection not working

$
0
0

Hi,

I'm guessing I made a really stupid mistake but I cannot see it for the life of me, and I hope someone can point out my stupidity!

I made up a user control to be placed into a ContentControl (ViewModel first approach) on a parent window. In this usercontrol, I just made only datagrid that is binded to a ObservableCollection which can be reused as a child view in other Parent Views. Now when the ViewModel is created, this child View is displaying, but there's no data in the grid.

This is the ViewModel:

    public class LocationCtrlViewModel  : BindableBase
    {
        private ObservableCollection<Location> _currentItem;

        public ObservableCollection<Location> CurrentItem
        {
            get { return _currentItem; }
            set { SetProperty(ref _currentItem, value); } // Implements INotifyPropertyChanged
        }

        public LocationCtrlViewModel()
        {}

        public LocationCtrlViewModel(List<Location> theList)
        {
            CurrentItem = new ObservableCollection<Location>(theList);
        }
    }

The Location class:

public class Location
{
        public int? LocId { get; set; } // LocID
        public string BinNo { get; set; } // BinNo
        public int? NoOfItems { get; set; } // NoOfItems
}

And finally the XAML

<UserControl x:Class="LocationCtrlView"
             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"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"><DataGrid x:Name="locationsDataGrid"
                  ItemsSource="{Binding CurrentItem}"
                  AutoGenerateColumns="False"><DataGrid.Columns><DataGridTextColumn x:Name="binNoColumn" Binding="{Binding BinNo}" Header="Bin No"
                                    Width="SizeToHeader" /><DataGridTextColumn x:Name="locationIDColumn" Binding="{Binding LocId}"
                                    Header="Location ID" Width="SizeToHeader" /><DataGridTextColumn x:Name="noOfItemsColumn" Binding="{Binding NoOfItems}" Header="No Of Items"
                                    Width="SizeToHeader" /></DataGrid.Columns></DataGrid></UserControl>

Now, I also tried putting the DataContext into the DataGrid itself where I have it like:

                  DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
                  ItemsSource="{Binding CurrentItem}"

But still nothing is showing. I put breakpoints at the constructor and indeed Location class values are being passed into the ViewModel.

So where am I going wrong?




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>