MVVM:如何避免将 DataContext 添加到 Xaml 以获得混合支持

发布于 2024-10-02 17:47:43 字数 930 浏览 2 评论 0原文

我们有一个基于松散 Xaml(或托管 Xaml - 从未确定正确术语!)的 C# 解决方案。但基本上我们获取一个 Xaml 文件并将其加载到 XamlReader 中,然后将该 FrameworkElement 弹出到 UserControl 上。我们需要处理数据绑定的能力,并且我们有一个 ViewModel 来处理这个问题。但是,虽然我可以通过在新创建的 FrameworkElement 上设置 DataContext 来通过代码注入 DataContext,但如果我想通过 Blend 轻松工作,并在选择“数据绑定...”选项时使用内置的可绑定属性列表,我需要在 Xaml 中设置 DataContext 属性(如果不是 Blend 会将其添加到 Xaml 中),我宁愿不必这样做,因为正如我所说,我已经通过代码执行此操作了

。有什么方法可以避免这种情况吗?

我正在考虑通过 app.xaml 以某种方式执行此操作,但问题取决于屏幕,并且当显示时它将具有不同的上下文(因此我们动态更改上下文)并且我'我非常确定您无法在多个数据上下文中加载。

也许有更好的方法来拆分它: -

  • 带有 MainWindow.xaml (主应用程序)的 WPF 应用程序
  • 单独程序集中的用户控件。由主 WPF 应用程序放置在 MainWindow.xaml 上(上图)。
  • 然后,该用户控件绑定到视图模型。
  • 主应用程序在状态机中构建时设置 DataContext。
  • 所有松散的 Xaml 文件都保存在主应用程序中。

因此,基本上根据状态机显示屏幕的时间,它将具有不同的数据上下文。状态机负责推送数据上下文,并且这些屏幕只有在正确的上下文中使用时才会工作。因此,我们实际上不需要(或想要)必须在 Xaml 中设置数据上下文。它由状态机完成。

但我在 Tinterweb 上找不到有关 Loose Xaml 的太多信息,因此我们正在不断学习...

提前致谢!

We have a C# solution that is based on loose Xaml (or Hosted Xaml - have never been to sure of the correct term!). But basically we take a Xaml file and load it into a XamlReader and then pop that FrameworkElement onto a UserControl. We require the ability to handle data binding and we have a ViewModel that takes care of this. However, although I can inject the DataContext through code by setting it on the newly created FrameworkElement, if I want to work easy through Blend and use the built in list of bindable properties when you select the 'Data Binding..." option on a property I need to have set the DataContext right there in the Xaml (if not Blend will add it to the Xaml). I'd rather not have to do this because as I say I am doing this through code already.

The question is - Is there any way to avoid this?

I was thinking of doing this somehow through the app.xaml but the problem is depending on the screen and when it is shown it will have a different context (so we dynamically change the context) and I'm pretty sure you can't load in multiple data contexts.

Maybe there's a better way of splitting this up. Our solution has the following: -

  • A WPF application with a MainWindow.xaml (the main application)
  • A User Control in a separate assembly that is placed on the MainWindow.xaml by the main WPF application (above)
  • This User Control is then bound to the View Model
  • The main application sets the DataContext as it goes through it's build in state machine.
  • All Loose Xaml files are held in the main application.

So basically depending on when the state machine displays a screen it will have a different Data Context. The State Machine handles pushing the Data Context and those screens will only work if they are used in the correct context. So we don't really need (or want) to have to set the data context in the Xaml. It done by the state machine.

But I haven't been able to find much info on the Tinterweb around Loose Xaml so we're pretty much learning as we go...

Thanks in advance!

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

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

发布评论

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

评论(1

短叹 2024-10-09 17:47:43

您是否尝试过 d:DataContext={d:DesignInstance ...} ?
它允许您仅在设计时为 Blend 和 VS 定义 DataContext。

Have you tried d:DataContext={d:DesignInstance ...} ?
It allows you to define a DataContext for design time only, for Blend and VS.

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