Avalondock MVVM

发布于 2024-11-04 03:52:17 字数 187 浏览 7 评论 0 原文

在 MVVM 环境中使用 avalondock 似乎相当具有挑战性。一旦我从 shellview 中分离 DocumentPane,我就会丢失相应的数据上下文,并且我的视图为空。重新连接后,又恢复正常。

有人有解决办法吗?

Using avalondock seems to be fairly challenging in a MVVM environment. As soon as I detach a DocumentPane from the shellview, I loose the corresponding datacontext and my view is empty. When reattaching, it gets back to normal.

Does anyone have a solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-11-11 03:52:17

这是我目前使用的;我的应用程序有几个可停靠窗格,每个窗格都有自己的视图模型。这些视图模型作为主窗口视图模型的属性公开,这也是主窗口的 DataContext。诀窍是使用静态 Application.Current 实例的 MainWindow.DataContext 来引用该数据上下文。

例如:

class MainWindowViewModel
{
  public WindowAViewModel {get; set;}
  public WindowBViewModel {get; set;}
}

  //this goes in App.xaml.cs, because my MainWindowViewModel has a constructor
  //with arguments, else you could just set the Window.DataContext in xaml
var window = new MainWindow();
window.DataContext = new MainWindowViewModel( ... );

MainWindow 的 xaml:

<ad:DockingManager>
  <ad:ResizingPanel Orientation="Horizontal" >
    <ad:DockablePane>
      <ad:DockableContent>
        <l:WindowA DataContext="{Binding Path=MainWindow.DataContext.WindowAViewModel,
                                 Source={x:Static app:App.Current}}"/>
      </ad:DockableContent>
      <ad:DockableContent>
        <l:WindowB DataContext="{Binding Path=MainWindow.DataContext.WindowBViewModel,
                                 Source={x:Static app:App.Current}}"/>
      </ad:DockableContent>
    </ad:DockablePane>
  </ad:ResizingPanel>
</ad:DockingManager>

虽然不确定大型模块化应用程序的效果如何,但我已经看到 示例应用程序与 Sofa(AvalonDock 包装器)一起使用,因此您可能想了解一下他们是如何做到的。

Here's what I use currently; my app hase a couple of dockable panes, each having their own viewmodel. These viewmodels are exposed as properties from the main window's viewmodel, which is also the DataContext of the main window. The trick is to refer to that datacontext using the static Application.Current instance's MainWindow.DataContext.

For example:

class MainWindowViewModel
{
  public WindowAViewModel {get; set;}
  public WindowBViewModel {get; set;}
}

  //this goes in App.xaml.cs, because my MainWindowViewModel has a constructor
  //with arguments, else you could just set the Window.DataContext in xaml
var window = new MainWindow();
window.DataContext = new MainWindowViewModel( ... );

MainWindow's xaml:

<ad:DockingManager>
  <ad:ResizingPanel Orientation="Horizontal" >
    <ad:DockablePane>
      <ad:DockableContent>
        <l:WindowA DataContext="{Binding Path=MainWindow.DataContext.WindowAViewModel,
                                 Source={x:Static app:App.Current}}"/>
      </ad:DockableContent>
      <ad:DockableContent>
        <l:WindowB DataContext="{Binding Path=MainWindow.DataContext.WindowBViewModel,
                                 Source={x:Static app:App.Current}}"/>
      </ad:DockableContent>
    </ad:DockablePane>
  </ad:ResizingPanel>
</ad:DockingManager>

Not sure how this works out for large modular applications though, but I've seen a sample application in Prism being used with Sofa (an AvalonDock wrapper) so you might want to check out how they did it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文