Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

In order to bind to a resource, you need to use the following syntax:

<Grid DataContext="{Binding {StaticResource Presenter}}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top