将 ObjectDataProvider 设置为视图的属性
我正在为我的 WPF 应用程序使用 MVP 模式。 我想将 ObjectDataProvider 设置为我在视图构造函数中设置的 Presenter 对象。然后我想将我的控件绑定到演示者的属性。
我已经像这样定义了我的 ObjectDataProvider:
<Window.Resources>
<ObjectDataProvider x:Key="pres" ObjectType="{x:Type local:MyPresenter}"/>
</Window.Resources>
<Grid DataContext="{Binding pres}" >
<ComboBox Name="_fileTypes" SelectedValuePath="Key" DisplayMemberPath="Value"
ItemsSource="{Binding Path=FileType}"/>
</Grid>
不幸的是
public partial class MyView : Window
{
public ViewPresenter MyPresenter { get; set; }
public Dictionary<int, string> FileNames { get; private set; }
public MyView()
{
InitializeComponent();
this.ViewPresenter = new MyPresenter(this, (IService)ObjectFactory.GetInstance<IService>());
this.FileType = GetFileTypes();
}
}
,ObjectDataProvider 似乎没有正确设置,我的 ComboBox 是空的,当我检查 this.Resources["pres"] 我得到:
{System.Windows.Data.ObjectDataProvider}
base {System.Windows.Data.DataSourceProvider}: {System.Windows.Data.ObjectDataProvider}
ConstructorParameters: Count = 0
IsAsynchronous: false
MethodName: null
MethodParameters: Count = 0
ObjectInstance: null
ObjectType: {Name = "MyPresenter" FullName = "Test.Presenters.MyPresenter"}
如何正确定义我的 ObjectDataProvider 来使用 MyPresenter我的视图的属性?
I am using the MVP pattern for my WPF application.
I would like to set the ObjectDataProvider to be the Presenter object that I setting in the constructor of my View. I then would like to bind to my controls to properties of the Presenter.
I have defined my ObjectDataProvider like this:
<Window.Resources>
<ObjectDataProvider x:Key="pres" ObjectType="{x:Type local:MyPresenter}"/>
</Window.Resources>
<Grid DataContext="{Binding pres}" >
<ComboBox Name="_fileTypes" SelectedValuePath="Key" DisplayMemberPath="Value"
ItemsSource="{Binding Path=FileType}"/>
</Grid>
and
public partial class MyView : Window
{
public ViewPresenter MyPresenter { get; set; }
public Dictionary<int, string> FileNames { get; private set; }
public MyView()
{
InitializeComponent();
this.ViewPresenter = new MyPresenter(this, (IService)ObjectFactory.GetInstance<IService>());
this.FileType = GetFileTypes();
}
}
Unfortunately the ObjectDataProvider does not seem to be set correctly, my ComboBox is empty and when I inspect this.Resources["pres"] I get:
{System.Windows.Data.ObjectDataProvider}
base {System.Windows.Data.DataSourceProvider}: {System.Windows.Data.ObjectDataProvider}
ConstructorParameters: Count = 0
IsAsynchronous: false
MethodName: null
MethodParameters: Count = 0
ObjectInstance: null
ObjectType: {Name = "MyPresenter" FullName = "Test.Presenters.MyPresenter"}
How should I correctly define my ObjectDataProvider to use the MyPresenter property of my View?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常设置视图的数据上下文。
publiv MyView()
{
this.DataContext = new Model();
}
类模型
{
公共 int SomeProperty{get;set;}
然后
,这将允许您绑定到模型上的属性,如下所示;
I normally set the view's data context.
publiv MyView()
{
this.DataContext = new Model();
}
class Model
{
public int SomeProperty{get;set;}
}
This would then allow you to bind to properties on the model like this;
为了绑定到资源,您需要使用以下语法:
In order to bind to a resource, you need to use the following syntax: