简体   繁体   English

Prism v4,MEF WPF DataBinding

[英]Prism v4, MEF WPF DataBinding

First, a few questions regarding databinding: 首先,关于数据绑定的几个问题:

  • Is the default DataContext for a control set to the codebehind? 控件的默认DataContext是否设置为代码隐藏? For example, if I have a variable orderNumber in test.xaml.cs, can I just reference it like so in the xaml {Binding orderNumber} ? 例如,如果我在test.xaml.cs中有一个变量orderNumber ,我可以在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. 我在一个单独的模块/程序集中有一个Prism服务,我通过MEF导入我的Shell应用程序。 I'm trying to databind on it but it doesn't appear to be working. 我正在尝试对它进行数据绑定,但它似乎没有起作用。

My workaround is below. 我的解决方法如下。 In my Shell.xaml.cs: 在我的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? 有没有办法在设置对象之前在xaml中设置datacontext - 特别是在MEF / Prism场景方面? On my ribbon object I tried DataContext="{Binding MenuService}" but that didn't work. 在我的功能区对象上,我尝试了DataContext="{Binding MenuService}"但这不起作用。

Is the default DataContext for a control set to the codebehind? 控件的默认DataContext是否设置为代码隐藏? For example, if I have a variable orderNumber in test.xaml.cs, can I just reference it like so in the xaml {Binding orderNumber}? 例如,如果我在test.xaml.cs中有一个变量orderNumber,我可以在xaml {Binding orderNumber}中像这样引用它吗?

No. By default, there is no DataContext, and its inherited from a parent using the hierarchy mechanisms in WPF. 否。默认情况下,没有DataContext,它使用WPF中的层次结构机制从父级继承。 You need to explicitly set the DataContext for a control, if you want it to have one. 如果需要控件,则需要为控件显式设置DataContext。

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 . 如果要使用双向绑定,则该对象还必须是DependencyObject或实现INotifyPropertyChanged

Is there a way to set the datacontext in xaml prior to an object being set - specifically in regards to MEF / Prism scenario? 有没有办法在设置对象之前在xaml中设置datacontext - 特别是在MEF / Prism场景方面? On my ribbon object I tried DataContext="{Binding MenuService}" but that didn't work. 在我的功能区对象上,我尝试了DataContext =“{Binding MenuService}”,但这不起作用。

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). 这将尝试使用层次结构将DataContext设置为包含DataContext的MenuService属性(即:父控件/窗口的DataContext的MenuService属性)。 You can't bind into yourself to set the DataContext. 您无法绑定到自己来设置DataContext。

You can create a new object in XAML for use as the DataContext, or have a containing object provide the DataContext for you. 您可以在XAML中创建一个新对象以用作DataContext,或者让一个包含对象为您提供DataContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM