Prism v4、MEF WPF 数据绑定
首先,关于数据绑定的几个问题:
- 控件的默认 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否。默认情况下,没有 DataContext,它使用 WPF 中的层次结构机制从父级继承。如果您希望控件具有 DataContext,则需要显式设置该控件。
是的。您只能绑定到属性。如果您希望双向绑定起作用,则该对象还必须是
DependencyObject
或实现INotifyPropertyChanged
。这将尝试使用层次结构将
DataContext
设置为包含DataContext 的MenuService
属性(即:父控件/窗口的DataContext 的MenuService 属性)。您无法绑定到自己来设置 DataContext。您可以在 XAML 中创建一个新对象用作 DataContext,或者让包含对象为您提供 DataContext。
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.
Yes. You can only bind to properties. If you want two way binding to work, the object must also be a
DependencyObject
or implementINotifyPropertyChanged
.This will attempt to set the
DataContext
to theMenuService
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.