Skip to main content

Microsoft Silverlight

Unanswered Question How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?RSS Feed

(0)

barukaSilver
barukaSi...

Member

Member

0 points

2 Posts

How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?

Here is my simple scenario:

click to download the source code here http://67.199.20.148/soccer/SLSoccerScore.zip

Here is the direct link to the live application

http://flairfornow.com/SLSoccerScore/SLSoccerScore/bin/Debug/Default.html

I am creating a silverlight 2 aplication to record a soccer game score :

> TEAM A(Brazil) vs TEAM B (France). 2 buttons will trigger the incrementation of the score data value.(one click for one goal etc..)

> the Class that define the data is quite simple as the following code below ( Featuring TeamName,Score and Coachname )

> Obviously the only value that will change during a soccer game will be the score value for each team.

>The scores will be shown in 2 textlock controls ( TextblockScoreTeamA and TextblockScoreTeamB ) .in a datagrid

> I designed a simple application with 2 buttons and 2 textblocks to begin with.

>I created the code to define the related data class and properties as seen below.

My problem is the folowing:

I want to be able to update the value of the score data whenever a buton is clicked,and that new score value should show up on the textblock related to the team that make the goal.(click event attached to the buttons of course)

I am wondering how to implement the INotifyPropertyChanged and ObservableCollection(of
Of SoccerTeam) in this context ??

any clue ??

/////////////////////////////////////////////////////

Partial Public Class Page

Inherits UserControl

Public Sub New()

InitializeComponent()

Dim SoccerTeamsList As List(Of SoccerTeam) = New List(Of SoccerTeam) 'this is equivalent of a database tables containing the soccer the 2 teams.

SoccerTeamsList.Add(New SoccerTeam() With {.TeamName = "Brazil", .Score = "0", .CoachName = "Orlando Arantes"})SoccerTeamsList.Add(New SoccerTeam() With {.TeamName = "France", .Score = "1", .CoachName = "Michel Platini"})

 

grdDisplay.ItemsSource = SoccerTeamsList

End Sub

End Class

Public Class SoccerTeam

Private _teamName As String

Public Property TeamName() As String 'setting teamName property

Get

Return _teamName

End Get

Set(ByVal value As String)

_teamName= value

End Set

End Property

Private _score As String

Public Property Score () As Integer 'setting teamName property

Get

Return _score

End Get

Set(ByVal value As Integer )

_score = value

End Set

End Property

Private _coachName As String

Public Property CoachName() As String 'setting teamName property

Get

Return _coachName

End Get

Set(ByVal value As String)

_coachName = value

End Set

End Property

 

End Class

 

//////////////////////////

click to download the source code here http://67.199.20.148/soccer/SLSoccerScore.zip

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17241 points

1,466 Posts

Re: How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?

 Hi barukaSilver,

barukaSilver:
click to download the source code here http://67.199.20.148/soccer/SLSoccerScore.zip
 

There is something wrong with the link.

barukaSilver:
I am wondering how to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in this context ??
 

 You can try to refer these links to implement the INotifyPropertyChanged and ObservableCollection,

  • http://www.cynotwhynot.com/blog/post/Silverlight-Data-Binding2c-using-INotifyPropertyChanged-and-ObservableCollection.aspx
  • http://weblogs.asp.net/jigardesai/archive/2008/11/02/display-live-data-in-silverlight-using-observablecollection-and-inotifypropertychanged.aspx 
Hope it helps.

 

 

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

barukaSilver
barukaSi...

Member

Member

0 points

2 Posts

Re: Re: How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?

Ok Folks ! May be I did n't explain correctely my issue here..

First of all look at the application I created  at this link..

http://www.flairfornow.com/soccer/SLSoccerScore/SLSoccerScore/bin/Debug/Default.html

As you can see,any time you click on a button (ie France) the text box will update the nue value .Obvioulsly the binding is working fine.

How ever look closely in the field of the table ,the original values set( 0) are still there.I want to be able to

change (I should say Update) the value in the table field the same way it change in the text box.!!

The step would be:

Button clicked---> the text box show the new value---> the new value is updated in the the grid .

Conclusion next time I restart the application the recent valu should then be reflected in the grid !!

Hope I make myself clear.

Any hint ? Thank you in advance.

Here is the link for the code(zip)

http://flairfornow.com/soccer/SLSoccerScore.zip

 

nasiraziz
nasiraziz

Member

Member

235 points

88 Posts

Re: Re: How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?

ObservableCollection already implements the INotifyProperty change. Any binding object should reflect any changes to the ObservableCollection list. Are you sure your binding is done properly?

EDIT:

Ok, I was able to see your VB code also (NotePad duh). Your Binding in XAML seems fine. In your VB code I see that you are not using the ObservableCollection.

In your case, you need a collection of SoccerTeam Class, you need to do this (I dont know VB syntax well, here is the C# version, hope you can do the VB on it):

ObservableCollection<SoccerTeam> soccerTeamList = new ObservableCollection<SoccerTeam> ();
soccerTeamList.Add(new SoccerTeam { Value1 = "something", Value2 = "Something" });

You can change the values of the list by finding the item in collection and replacing its values:

var soccerTeam = (from s in soccerTeamList
                            where s.TeamID = whatever
                            select s).FirstOrDefault();

soccerTeam.TeamScore = 10; //for example

This will reflect in the ObservableCollection and values will change in your grid. Hope this helps.

-------------------------------------------------
{DeVigner} :: http://blog.geomentary.com/

Matthew R Hall
Matthew ...

Member

Member

12 points

6 Posts

Re: Re: How to implement the INotifyPropertyChanged and ObservableCollection(of Of SoccerTeam) in my project ?

Its also useful to remember that its adding and removing objects to the observableCollection that raises the Notify event. If you modify the ObservableCollection in other ways (assigning one ObservableCollection to another) you'll want to raise the CollectionChanged event.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities