开发者

How can i pass an object from one class to another in WPF (MVVM)?

开发者 https://www.devze.com 2023-02-22 22:03 出处:网络
I´m learning the MVVM for WPF. I know how to rout events and split the Code in View, ViewModel, Model. I have in my main-XAML a TabControl and have split the tabs into different views.

I´m learning the MVVM for WPF. I know how to rout events and split the Code in View, ViewModel, Model. I have in my main-XAML a TabControl and have split the tabs into different views. My Question is, how can i pass an object from one class to another? (From the MainWindow.cs to the SubWindow.cs)

MainWindowRessources XAML:

....
<DataTemplate DataType=开发者_如何学JAVA"{x:Type vm:SubWindow}">
  <vw:SubWindow />
</DataTemplate>

<vm:SubWindow x:Key="subView" />
..

MainWindow XAML:

<Window.Resources>
  <ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>

...
..
<TabItem>
        <ContentControl Content="{StaticResource subView}" />
</TabItem>
...
..


You should consider implementing the mediator pattern to allow your view models to communicate with each other.

See this Stackoverflow answer for more information.


Depending on the context/use, you could create a DependencyProperty on the SubWindow class, and pass the object as a parameter, ala <vm:SubWindow MyNewProperty="some-value-or-object here"/>.

Info about creating DependencyProperties: http://msdn.microsoft.com/en-us/library/ms752914.aspx

What is the object you're wanting to pass?

0

精彩评论

暂无评论...
验证码 换一张
取 消