0

WPF アプリケーションに MVP パターンを使用しています。ビューのコンストラクターで設定する Presenter オブジェクトに ObjectDataProvider を設定したいと思います。次に、コントロールをプレゼンターのプロパティにバインドしたいと思います。

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"}

View の MyPresenter プロパティを使用するには、ObjectDataProvider を正しく定義するにはどうすればよいですか?

4

2 に答える 2

0

リソースにバインドするには、次の構文を使用する必要があります。

<Grid DataContext="{Binding {StaticResource Presenter}}" />
于 2011-03-16T11:46:02.413 に答える
0

通常、ビューのデータ コンテキストを設定します。

publiv MyView() { this.DataContext = 新しいモデル(); }

クラス モデル { public int SomeProperty{get;set;} }

これにより、次のようにモデルのプロパティにバインドできます。

于 2011-03-17T10:54:33.210 に答える