Reputation: 189
I created a new Window which I then call using the following code:
NieuwSimulatie NiewSimulatieWindow = new NieuwSimulatie()
{
Owner = this
};
bool? SimulatieAangemaakt = NiewSimulatieWindow.ShowDialog();
In the Window "NieuwSimulatie" I'd like to have the same DataContext as in my MainWindow, or I would like to DataBind the controls directly to the DataContext using RelativeSource I geuss, Ive tried:
<Controls:SplitButton x:Name="ProjectNaam"
DisplayMemberPath="ProjectNaam"
ItemsSource="{Binding Static.Projecten, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Controls:MetroWindow}}"/>
But this doesn't work.
Please help me either fix my DataBinding or help me set the DataContext of my Window to the DataContext that is behind MainWindow.
Upvotes: 0
Views: 1894
Reputation: 1
I think you need to do it like this :
NieuwSimulatie NiewSimulatieWindow = new NieuwSimulatie()
NiewSimulatieWindow.DataContext=this.DataContext;
bool? SimulatieAangemaakt = NiewSimulatieWindow.ShowDialog();
Upvotes: 0
Reputation: 6963
To do what you ask is typically done when creating the new window, because they are two Windows it's better to pass the context in...like this:
NieuwSimulatie MyNewWindow = new NieuwSimulatie(DataContent);
MyNewWindow.Show();
Upvotes: 0
Reputation: 229
I've you have acces to your DataContext from where you create your window, how about that:
NieuwSimulatie NiewSimulatieWindow = new NieuwSimulatie()
{
Owner = this,
DataContext = YourDataContext;
};
bool? SimulatieAangemaakt = NiewSimulatieWindow.ShowDialog();
Upvotes: 1