开发者

Prism v4, MEF WPF DataBinding

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜