Click here to Skip to main content
15,908,175 members

Comments by chawk32 (Top 6 by date)

chawk32 22-Dec-12 18:10pm View    
Hi Christian, So using your suggestion about creating a viewmodel that multiple usercontrols can use I successfully binded one of the usercontrols to two of my properties but the other usercontrol does not bind to one of my properties. Here is some code snippets: First the viewmodel,

public class TournamentModel : INotifyPropertyChanged
{
// Rounds List
private ObservableCollection<rounds> _roundsList = new ObservableCollection<rounds>();
public event PropertyChangedEventHandler PropertyChanged;

private static readonly PropertyChangedEventArgs TextArgs = new PropertyChangedEventArgs("TournamentRounds");

public ObservableCollection<rounds> TournamentRounds
{
get { return _roundsList; }
set
{
_roundsList = value;
NotifyChange(TextArgs);
}
}

private void NotifyChange(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}

Now the usercontrol (TeamRoundMatchupUserControl.xaml) which is trying to bind to TournamentModel.TournamentRounds.
<combobox itemssource="{Binding TournamentRounds}" displaymemberpath="Name" height="23" horizontalalignment="Center" margin="356,41,416,0" name="roundNameComboBox" verticalalignment="Top" width="178">

The TeamRoundMatchupUserControl.cs class.
private readonly TournamentModel _tournamentModel;

public TeamRoundMatchupUserControl()
{
InitializeComponent();

_tournamentModel = new TournamentModel();
this.DataContext = _tournamentModel;
}

Note: As a test I changed the binding to look at a property called CurrentTournament
which is also in the TournamentModel class and it worked fine. Thoughts?
chawk32 19-Dec-12 16:43pm View    
While I am learning MVVM and data binding in WPF, I am trying to do it the "correct" way so I was purposely avoiding doing things how I knew I could make work and instead try to figure out ways that may end of being more beneficial in the long run. Thanks for your help. It is much appreciated.
chawk32 19-Dec-12 16:35pm View    
See that is the part that I didn't understand. I thought using MVVM that each User Control should have its own View Model. I believe I have read that elsewhere. Can you point to two different View Models from one User Control?
chawk32 19-Dec-12 16:26pm View    
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.
chawk32 19-Dec-12 16:16pm View    
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.