c#


Binding a List<Class> to a ListBox


I have a List which contains a few items of this class:
public class Entry
{
public string FileSize;
public string Name;
public byte[] Data;
public UInt64 XXHashFilePath;
public UInt32 FileDataOffset;
public UInt32 CompressedSize;
public UInt32 UncompressedSize;
public CompressionType TypeOfCompression;
public UInt64 SHA256;
public Entry(BinaryReader br)
{
XXHashFilePath = br.ReadUInt64();
FileDataOffset = br.ReadUInt32();
CompressedSize = br.ReadUInt32();
UncompressedSize = br.ReadUInt32();
TypeOfCompression = (CompressionType)br.ReadUInt32();
SHA256 = br.ReadUInt64();
FileSize = Functions.SizeSuffix(UncompressedSize);
}
}
I want my ListBox to display XXHashFilePath, TypeOfCompression, FileSize, Name in columns in that specific order.
You have change your fields to properties (like Clemens proposed). This is also a principle of OOP with c# https://msdn.microsoft.com/en-us/library/ms229057(v=vs.110).aspx
Furthermore you may want to implement the INotifyPropertyChanged is just necessary when properties change their value after an Entry was added to the items collection of a ListBox. If Entry elements are immutable and just added to (and removed from) a ListBox, you won't have to implement INotifyPropertyChanged.
public class EntryViewModel : ViewModelBase
{
private string _FileSize;
public string FileSize
{
get
{
return _FileSize;
}
set
{
_FileSize = value;
OnPropertyChanged();
}
}
}
It's also a best practice to use the MVVM pattern. Here is a simple base class that should get you started without using a MVVM-framework.
public class ViewModelBase : INotifyPropertyChanged
{
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion Events
#region Methods
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion Methods
}

Related Links

Is it possible to skip overriden methods code when based on base code
How to parse nested JSON data structure
EWS C# - Null FileName returned
WCF between ASP MVC 5 web application and winforms application [closed]
Windows service with RegisterDeviceNotification
Extract html nodes having not specific child or tags by HTML Agility?
Confused about XML serializer and mixed accessibility Properties
How to use ServiceStack Redis API?
How to see service time in glimpse
EntityFramework and bad table name
How to Parse a date string from a text file on PC to DateTime
Open content of Tabitem in new Window
Why is this connection string invalid?
Create Hyperlink in TextBlock via Binding
Can't understand How to decide when to use Hidden() and when to use HiddenFor()
Accessing another user calendar using the O365 REST API from a single domain admin account

Categories

HOME
indexing
twitter-bootstrap
webvr
aix
ibm
skypedeveloper
highmaps
spss
opc-ua
google-docs-api
css-selectors
python-3.4
obd-ii
token
soap-client
coding-style
activejdbc
ndepend
urllib2
dynamics-crm-2011
ibatis
wallet
minimagick
ejb-3.1
aws-certificate-manager
android-6.0-marshmallow
multiple-inheritance
boosting
recreate
schedule
epub
machine-code
matlab-compiler
android-databinding
instances
ui-grid
flume-ng
chrome-custom-tabs
seyren
selection-sort
rmq
disassembly
scrollmagic
sqlproj
mavlink
indoor-positioning-system
tinybox2
nimbus
django-taggit
innertext
calc
hana-sql-script
clear
cascading
updating
dnvm
eggplant
connection-timeout
yosemite
pytz
javacompiler
livescript
rpt
visual-c#-express-2010
hotlinking
sendy
email-headers
data-layers
kuka-krl
autofilter
mobile-robots
cab
ia-32
record-locking
qgraphicsscene
linqdatasource
quit
freeimage
mail-gem
shapado
waveout
google-email-audit-api
sup
flatten
win-prolog
towerjs
fileoutputstream
quazip
excellibrary
rollover
azure-appfabric
mysql-pconnect
calendarextender
morfik
zend-acl

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile