Ruven Graf
Ruven Graf

Reputation: 11

Access DataContext of Page from MainWindow (with Telerik)

I am relatively new to WPF and I have stumbled across a problem that I just can't seem to find a solution for.

I am sure that there is already a thread concerning a problem like that but in regard of my lacking knowledge it is very likely that I haven't found it or simply did not understand it.

My problem:

I am developing a WPF-application in C#. It's an Outlook-Styled application with a big MainWindow with a huge ViewModel and XAML. What I was trying to do, is to split up the single codefiles a bit to make it a little bit more modular and compact. I am using Telerik Controls and tried to outsource the content of single SplitContainers into Pages, which worked fine until now.

Today, a new situation came up which is somehow stupid and wasn't looking too complicated, but somehow I can't get it to work.

Situation:

I have a Treeview in my "MainWindow" and whenever I change the selection in there, I want to change a property on my Page that I have made a binding to.

So, when I click on an item in "TreeView_3" I want to set a property via EventHandler (SelectionChanged_TreeView3) on the DataContext of "Page_X".

If I had to do this on the MainWindow, I would typically do it like that:

UserViewModel uvm = mainGrid.DataContext as UserViewModel;

Then just call whatever property of specific UserViewModel (ViewModel of the MainWindow) I want to access.

I can't do this the same the same way for the page obviously since "mainGrid.DataContext" will always refer to the MainWindow, since this is where the eventhandler is called.

So what I need would be a little explanation on how to access the DataContext from a page with a different ViewModel.

If you need any code in order to explain, let me know.

Upvotes: 0

Views: 175

Answers (2)

ChristianMurschall
ChristianMurschall

Reputation: 1711

You need to separate your concerns. In your code behind your should have only code that handles view related stuff. Most often my codebehind is empty.

In your ViewModels you should handle your data related logic. So instead of casting the datacontext in your code behind, handle a click with a Commandin your viewmodel.

Since there is no possibility to bind a command to the SelectedItemChanged of your TreeView you can use an interaction trigger.

<TreeView xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectedItemChanged">
            <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TreeView>

Upvotes: 0

Timmy Fuller
Timmy Fuller

Reputation: 75

Ruven it is hard to say without some example code. But it could be that you need to implement INotifyPropertyChanged on the ViewModels?

https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification

By calling OnPropertyChanged("PropertyName"); in the setter of a viewmodel property the ui will pick up the change.

Also make sure both views are referencing the same object and not copies of the same object.

Upvotes: 0

Related Questions