将 ObjectDataProvider 设置为视图的属性

发布于 2024-10-22 02:46:23 字数 1421 浏览 2 评论 0原文

我正在为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-10-29 02:46:23

我通常设置视图的数据上下文。

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;

不爱素颜 2024-10-29 02:46:23

为了绑定到资源,您需要使用以下语法:

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

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

<Grid DataContext="{Binding {StaticResource Presenter}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文