user3692104
user3692104

Reputation: 189

How to set DataContext in a Dialog Window to its parent's DataContext

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

Answers (3)

Moner Kamal
Moner Kamal

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

JWP
JWP

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

coder0815
coder0815

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

Related Questions