Jess Chan
Jess Chan

Reputation: 439

How do I set the DataContext to multiple classes?

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?

Upvotes: 2

Views: 2487

Answers (2)

mm8
mm8

Reputation: 169420

You can only set the DataContext property to a single object, but the type of this object may contain multiple properties that you can bind to:

class ViewModel
{
    public Player Player { get; } = new Player();
    public Achievement Achievement { get; } = new Achievement();
}

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

XAML:

<TextBox Text="{Binding Path=Person.Firstname}" />
...
<TextBlock Text="{Binding Achievement.SomeProperty}" />

Upvotes: 8

Hubii
Hubii

Reputation: 348

As Best you create different List's like this:

  public ObservableCollection<Player> ListOne = new ObservableCollection<Player>();

after that you fill this list with your values.

now you Need to give your TextBox in XMAL a DataTemplate with The Class Player.

                  <TextBox.ItemTemplate>
                        <DataTemplate x:DataType="data:Bilder">
                            //Whatever you want to bind here.
                        </DataTemplate>
                    </TextBox.ItemTemplate>

At the end you can bind now different Lists to different Controls.

TbName.source = ListOne;

Upvotes: -4

Related Questions