So I am just getting into databinding & MVVM and I am having a small issue with this one thing.
I have a WPF project with a seperate class called Player
In my MainWindow
class I am setting the DataContext
to a instance of that Player
class
public partial class MainWindow : Window
{
Player player = new Player();
public MainWindow()
{
InitializeComponent();
DataContext = player;
}
}
In that class I have a property in which I am setting a value in the constructor.
public class Player : INotifyPropertyChanged
{
private string _Firstname;
public Player()
{
_Firstname = "William";
}
public string Firstname
{
get { return _Firstname; }
set { _Firstname = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
And then in the XAML there is some simple databinding logic going on for the TextBox
<TextBox Name="TbName" HorizontalAlignment="Left" Height="23" Margin="243,119,0,0" TextWrapping="Wrap" Text="{Binding Path=Firstname}" VerticalAlignment="Top" Width="120"/>
Now.. Let's say I had another class called I don't know.. Acheivments..
How would I set the Text
of another control to a property of that class? I would have to set another DataContext
and I don't know how to set multiple datacontexts.
QUESTION: How do I properly set two datacontexts so I can bind different classes to different controls?