Prism v4、MEF WPF 数据绑定

发布于 2024-10-17 09:16:00 字数 1059 浏览 2 评论 0原文

首先,关于数据绑定的几个问题:

  • 控件的默认 DataContext 是否设置为代码隐藏?例如,如果我在 test.xaml.cs 中有一个变量 orderNumber,我可以像在 xaml {Binding orderNumber} 中那样引用它吗?
  • 我只能对对象的属性进行数据绑定,这是否正确?

我在单独的模块/程序集中有一个 Prism 服务,我通过 MEF 将其导入到我的 Shell 应用程序中。我正在尝试对其进行数据绑定,但它似乎不起作用。

我的解决方法如下。 在我的 Shell.xaml.cs 中:

[Import(AllowRecomposition = false)]
private IRibbonService _menuService;
public IRibbonService MenuService
{
    get
    {
        return _menuService;
    }
}


public void OnImportsSatisfied()
{
    Debug.WriteLine("Imports satisfied", "Prism");
    this._moduleManager.LoadModuleCompleted += new EventHandler<LoadModuleCompletedEventArgs>(moduleManager_LoadModuleCompleted);

    //TODO figure out how to properly bind to the ribbon
    Ribbon.DataContext = _menuService;
    RibbonAppMenu.DataContext = _menuService.ApplicationMenuData;
}

有没有办法在设置对象之前在 xaml 中设置数据上下文 - 特别是在 MEF / Prism 场景中?在我的功能区对象上,我尝试了 DataContext="{Binding MenuService}" 但这不起作用。

First, a few questions regarding databinding:

  • Is the default DataContext for a control set to the codebehind? For example, if I have a variable orderNumber in test.xaml.cs, can I just reference it like so in the xaml {Binding orderNumber}?
  • Is it correct that I can only databind to properties of an object?

I have a Prism service in a separate module/assembly that I import into my Shell application via MEF. I'm trying to databind on it but it doesn't appear to be working.

My workaround is below.
In my Shell.xaml.cs:

[Import(AllowRecomposition = false)]
private IRibbonService _menuService;
public IRibbonService MenuService
{
    get
    {
        return _menuService;
    }
}


public void OnImportsSatisfied()
{
    Debug.WriteLine("Imports satisfied", "Prism");
    this._moduleManager.LoadModuleCompleted += new EventHandler<LoadModuleCompletedEventArgs>(moduleManager_LoadModuleCompleted);

    //TODO figure out how to properly bind to the ribbon
    Ribbon.DataContext = _menuService;
    RibbonAppMenu.DataContext = _menuService.ApplicationMenuData;
}

Is there a way to set the datacontext in xaml prior to an object being set - specifically in regards to MEF / Prism scenario? On my ribbon object I tried DataContext="{Binding MenuService}" but that didn't work.

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

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

发布评论

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

评论(1

枯叶蝶 2024-10-24 09:16:00

控件的默认 DataContext 是否设置为代码隐藏?例如,如果我在 test.xaml.cs 中有一个变量 orderNumber,我可以像在 xaml {Binding orderNumber} 中那样引用它吗?

否。默认情况下,没有 DataContext,它使用 WPF 中的层次结构机制从父级继承。如果您希望控件具有 DataContext,则需要显式设置该控件。

我只能对对象的属性进行数据绑定,这样正确吗?

是的。您只能绑定到属性。如果您希望双向绑定起作用,则该对象还必须是 DependencyObject 或实现 INotifyPropertyChanged

有没有办法在设置对象之前在 xaml 中设置数据上下文 - 特别是关于 MEF / Prism 场景?在我的功能区对象上,我尝试了 DataContext="{Binding MenuService}" 但这不起作用。

这将尝试使用层次结构将DataContext 设置为包含DataContext 的MenuService 属性(即:父控件/窗口的DataContext 的MenuService 属性)。您无法绑定到自己来设置 DataContext。

您可以在 XAML 中创建一个新对象用作 DataContext,或者让包含对象为您提供 DataContext。

Is the default DataContext for a control set to the codebehind? For example, if I have a variable orderNumber in test.xaml.cs, can I just reference it like so in the xaml {Binding orderNumber}?

No. By default, there is no DataContext, and its inherited from a parent using the hierarchy mechanisms in WPF. You need to explicitly set the DataContext for a control, if you want it to have one.

Is it correct that I can only databind to properties of an object?

Yes. You can only bind to properties. If you want two way binding to work, the object must also be a DependencyObject or implement INotifyPropertyChanged.

Is there a way to set the datacontext in xaml prior to an object being set - specifically in regards to MEF / Prism scenario? On my ribbon object I tried DataContext="{Binding MenuService}" but that didn't work.

This will attempt to set the DataContext to the MenuService property of the containing DataContext using the hierarchy (ie: the parent control/window's DataContext's MenuService property). You can't bind into yourself to set the DataContext.

You can create a new object in XAML for use as the DataContext, or have a containing object provide the DataContext for you.

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