在WPF中使用MVVM的Oxyplot实现-嵌套视图?

时间:2018-10-22 20:40:40

标签: c# wpf xaml mvvm oxyplot

tl; dr参见底线,我将在这里描述我的结构和MVVM实现:

我实际上是在尝试在现有的WPF应用程序中实现一个Oxyplot图,但是我仍然坚持如何正确地嵌入它。我可以直接在XAML / codebehind中使用框架提供的Plotmodel类来使其工作,但是,这似乎不太干净,并导致了绑定和数据封装方面的一些问题。

他们提供了一些有关使用视图模型方法的WPF / MVVM的示例和docs-这导致了我的特定问题:

在我的应用中,我使用像这样的Shell View / VM,托管一个ContentControl来容纳其他View(仅相关部分):

ApplicationView.xaml

<Window x:Class="MyApp.Views.ApplicationView"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:vm="clr-namespace:MyApp.ViewModels"
   xmlns:v="clr-namespace:MyApp.Views">

 <Window.Resources>
     <DataTemplate DataType="{x:Type vm:HomeViewModel}">
         <v:HomeView />
     </DataTemplate>
     <DataTemplate DataType="{x:Type vm:DataInputViewModel}">
         <v:DataInputView />
     </DataTemplate>
 </Window.Resources>

 <ContentControl BorderThickness="10" BorderBrush="#444444" Grid.Row="0" Grid.ColumnSpan="3" Content="{Binding CurrentPageViewModel}"/>

 // Other control elements, e.g. for navigation through the Views
</Window>

在相应的ApplicationViewModel.cs中,我要切换活动的ViewModel。规范中只有App.xaml.cs中的基本内容。初始化:

App.xaml.cs

namespace MyApp
{

  public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);

          Views.ApplicationView app = new Views.ApplicationView();
          ViewModels.ApplicationViewModel context = new ViewModels.ApplicationViewModel();
          app.DataContext = context;
          app.Show();
      }
  }

}

现在,我想将Oxyplot控件嵌入到特定的SubView中,该子视图的设置如下:

ExampleView.xaml

<UserControl x:Class="MyApp.Views.ExampleView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:hc="clr-namespace:MyApp.HelperClasses"
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d">

//much of elements and stuff binding to ExampleViewModel.cs

但是,我不确定如何在此处嵌入绑定到Oxyplot的控件。在上面提到的文档中,他们通过设置自己的View和ViewModel来解释通过MVVM布局进行的基本集成。 这使我想知道是否有可能(并且不是一个不好的做法!?)在该视图中嵌入另一个具有自己的ViewModel的视图。在这种情况下:如何使用这种结构切换DataContext?

是的,我已经阅读了贯穿线程,问题和示例,但是在阅读了一些关于变态准则违反嵌套视图魔术知识的不确定意见后,我变得更加不安全。

0 个答案:

没有答案