Click here to Skip to main content
15,902,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of objects I'm returning from a database. Should I be loading them into two lists - one an observablecollection with the fields that I want to display, and another with all the other fields that won't be shown. Or can I just load everything into the ObservableCollection, and simply not raise a PropertyChanged event on the properties that won't be displayed?

(BTW: I'm way to new at WPF to be implementing the MVVM pattern, so any MVVM based comments will likely be wasted on me).

Can I be lazy and do this?
C#
{
var Foos = new ObservableCollection<Foo>();
// load Foo items from database
}

class Foo 
{
	public int FooID { get; set;}
	//... plus a bunch of properties I won't be displaying
	
	private string _fooName;
	public string FooName
	{
		get
		{
			return _fooName;
		}
		set
		{
			if (_fooName != value)
			{
				_fooName = value;
				OnPropertyChanged();
			}
		}
	}
	//... plus a bunch more of properties I will be displaying
}


Or should I repeat myself for the sake of binding performance and do this?

C#
{
	var foos = new List<Foo>();
	// load FoosToDisplay from database
	var FoosToDisplay = new ObservableCollection<FooToDisplay>();
	// load FoosToDisplay from foos

}

class Foo
{
	public int FooID { get; set;}
	//... plus a bunch of properties I won't be displaying
	public string FooName { get; set; }
	//... plus a bunch more of properties I will be displaying
}

class FooToDisplay
{
	private string _fooName;
	public string FooName
	{
		get
		{
			return _fooName;
		}
		set
		{
			if (_fooName != value)
			{
				_fooName = value;
				OnPropertyChanged();
			}
		}
	}
	//... plus a bunch more of properties I will be displaying
}


Or can FooToDisplay inherit from Foo?

What I have tried:

Haven't tried anything. Need advice before I start.
Posted
Updated 7-Dec-17 21:09pm

It's fine to have a bunch of properties that you aren't going to be displaying in your OC, and you could even implement INotifyPropertyChanged on them if you wanted to. As long as you don't hook them up to XAML, any property changes are going to be ignored.
 
Share this answer
 
The easiest is to use a CollectionViewSource to filter the items that you dont want to show away.
CollectionViewSource Class (System.Windows.Data)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900