Click here to Skip to main content
15,885,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my first post, so if I don't explain or ask my questions correctly I apologize in advance. I am working on an application that contains a class with multiple user controls that I am using as Tab Items. Each of the User Controls contains other controls like buttons and text boxes and the like. Code snippet below:

Window x:Class="TeamShootout.TeamShootoutMainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Tournaments="clr-namespace:TeamShootout.Tournaments" 
    xmlns:RoundMatchup="clr-namespace:TeamShootout.TeamRoundMatchup" 
    xmlns:TeamScoring="clr-namespace:TeamShootout.TeamScoring" 
    xmlns:BestBallScoring="clr-namespace:TeamShootout.BestBallScoring" 
    xmlns:BestBallRoundMatchup="clr-namespace:TeamShootout.BestBallRoundMatchup" 


XML
<TabControl>
                <TabItem Header="Tournaments">
                    <Tournaments:TournamentsUserControl />
                </TabItem>
                <!--Controls for the Round Matchup tab.-->
                <TabItem Header="Team Matchup">
                    <RoundMatchup:TeamRoundMatchupUserControl />
                </TabItem>
                <TabItem Header="Team Scoring">
                    <TeamScoring:TeamScoringUserControl />
                </TabItem>
                <TabItem Header="Best Ball Matchup">
                    <BestBallRoundMatchup:BestBallRoundMatchupUserControl />
                </TabItem>
                <TabItem Header="Best Ball Scoring">
                    <BestBallScoring:BestBallScoringUserControl />
                </TabItem>
            </TabControl>


What I am trying to accomplish is when the user selects some value on one of the tabs that that value is binded to another Tab Item/User Control, so when the user goes to that tab the associated User Control knows what was selected. For example, if the user selects a tournament from the Tournaments Tab Item, I want that tournament number to be available to the Team Matchup Tab Item. I am successful in binding within a User Control but not between User Controls. Suggestions?
Posted
Updated 19-Dec-12 10:42am
v2

It sounds to me like you want to store your data in a collection that all your tabs bind to. That way, it's one data store that contains your data, and each tab renders from a common data source.
 
Share this answer
 
Comments
chawk32 19-Dec-12 16:00pm    
Christian: I currently have the data in a collection, but the part that I am struggling with is how to bind to that collection which is in one user control and have another user control have access to it.
Christian Graus 19-Dec-12 16:02pm    
I don't understand the issue. If you have a collection, seperate from the tab page, why can't both tabs access it ?
chawk32 19-Dec-12 16:16pm    
The issue is I don't know how to bind from one User Control to another so that I can access properties within a User Control.
Christian Graus 19-Dec-12 16:18pm    
Again - you don't do that at all. Both user controls bind to a collection of data, which they have in common.
chawk32 19-Dec-12 16:26pm    
Sorry Christian, I didn't explain myself very well. For the TournamentUserControl I have a class TournamentsViewModel. That is where the collection resides. I would like TeamRoundMatchupUserControl to be bound to that collection. BTW, TeamRoundMatchupUserControl has its own view model.
You need a data model class that exposes your data collections and selected items. It should implement the INotifyPropertyChanged interface and it should be static or implement the Singleton pattern if you will use it across multiple pages or views.

That class should expose your data collections as a property and hold a single item as the "selected" property. The selected property should make sure that it raises the PropertyChanged event when the property is set. Your UI controls will respond to that event when bound to that property.

You then need to set the data model class as your data context for the controls/page. You can either do this in the code behind by setting this.DataContext = new DataModelClass; or by declaring the data context in xaml and setting it to an instance of you data model class.

Once you have done that, you can bind the properties to the properties on the data model class. For example, for a combobox the xaml may be:
C#
<combobox selecteditem="{Binding SelectedItem, Mode=TwoWay}" />
That tells the binding engine to wire up the selected item property on the combobox to the property on your data model class and make the relationship two way. If you do this on one tab then bind a text box or some other control on another tab (skipping the mode statement if it only needs read access) then when the selected item in the combo box is changed, the text box text on the other tab will change.


Bindings are fundamental to xaml. I would suggest you read-up on data bindings, especially some of the stuff around MVVM, to help get a better understanding. What you are asking is pretty straightforward and can be done lots of way... I've suggested one... But you need to understand each approach as it can have tradeoff in ease of development/maintenance versus performance and scalability.

You might want to check out Tim Heuer's post on data binding in Silverlight at http://timheuer.com/blog/articles/getting-started-with-silverlight-development.aspx[^] While it is for Silverlight, the principles are the same.
 
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