C# - Set ObjectDataProvider to be Property of the View
Translations
Englishالعربية
български
català
中文
čeština
dansk
Nederlands
eesti
suomi
français
Deutsch
Ελληνικά
עברית
हिंदी
magyar
Bahasa Indonesia
italiano
日本語
한국어
latviešu
lietuvių
norsk
polski
Português
română
русский
slovenčina
slovenski
español
svenska
ไทย
Türkçe
українська
Tiếng Việt
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?
This question and answers originated from www.stackoverflow.com
Question by openshac (3/16/2011 11:00:18 AM)
Answer |
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;
Answer by James Miles
Find More Answers
Related Topics c# binding mvp objectdataprovider