Silverlight / WPF - How can I show total value from 6 textboxes into box No7 Xaml form or And code behind

Asked By David on 23-Dec-12 04:37 PM
 Hi All
 I would like to show on the bottom of a Xaml form the sum of 6 seperate textboxes all containing numbers and show in a 7th textbox the total they add up to.
Can this be done in the Xaml ? or does it need code in the Xaml.vb if so what goes where.

Thanks

David
Robbe Morris replied to David on 23-Dec-12 05:08 PM
Create a property on your data class for the total.  In the property get/sets of the other 6 properties, raise the PropertyChanged event on your total property.  The data binding in XAML will take care of updating the user interface.
David replied to Robbe Morris on 24-Dec-12 05:30 AM

 Hi Robbie

 Thanks for your quick replyI have the following code in the XAML.vb

Private


Sub TextBoxscore_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxscore.TextChanged

TextBoxscore.Text = txtH1SC.Text + TxtH2SC.Text  and so on for the rest of textboxes


End Sub

End Class

 The Xaml form I have the following
 

TextBox Grid.Column="3" Name="TextBoxscore" VerticalAlignment="Bottom" Text="{Binding Path=TextBoxScore, UpdateSourceTrigger=PropertyChanged}"/>

Can you point me in the right direction please
Thanks David
Robbe Morris replied to David on 24-Dec-12 08:59 AM
No, you are binding to itself.  The core feature of XAML is the ability to data bind to your data class, list of data class, or some other datasource that isn't the XAML TextBoxes, ComboBoxes, etc...

Here this might help,

http://www.eggheadcafe.com/tutorials/xaml/fb496607-14b3-4f0a-af9c-71731557c486/wpf--silverlight-dynamically-change-data-binding-attributes-at-runtime.aspx
David replied to Robbe Morris on 30-Dec-12 04:48 PM
 Hi Robbie

 I can bind from any single textbox ie( textbox2,textbox3 etc ) into textbox1 on the Xaml form but cant find out how to add the values of more than 1 textbox at a time Any ideas please I dont seem to be able to do it.
I need to add the values of 6 textboxes into a running total.

David

Robbe Morris replied to David on 30-Dec-12 05:05 PM
You've got to stop thinking in terms of binding UI elements to one another and expecting great things to happen.  That's how things were done with Windows Forms .NET.  The key in WPF is to bind to a separate data class with its own properties.  As the data in each property changes, you can trigger the UI to refresh itself via the PropteryChanged event.

In WPF, do not try to bind one or more textboxes to another.  It just doesn't work that way.  In WPF, you focus on changing the underlying data and let the magic of databinding refresh the user interface.

Read the article I posted above.
David replied to Robbe Morris on 01-Jan-13 04:16 PM
 Robbe

Thanks for you patience but I am still at a loss how to create or use a dataclass.

This is my Xaml vb page where I pick up textbox values etc can i hook up to this and if so how?

Class Window3

Private COURSESPLAYEDData As New PlayedCoursesDataSet

Private taCOURSESPLAYED As New PlayedCoursesDataSetTableAdapters.COURSES_PLAYEDTableAdapter

Private taMANAGER As New PlayedCoursesDataSetTableAdapters.TableAdapterManager

Private Placeholder As String

Private view As CollectionView

Private strCourseName As String 'used in the constructor - get selected course name from Window12

Private strCourseId As Integer 'used in the constructor - get selected course name from Window12

Public Shadows GotKeyboardFocus As KeyboardFocusChangedEventHandler

 Thanks
David