开发者

Set ObjectDataProvider to be Property of the View

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:Typ开发者_高级运维e 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?


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:

<Grid DataContext="{Binding {StaticResource Presenter}}" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜