使用对象数据提供者

发布于 2024-07-15 12:01:09 字数 1099 浏览 5 评论 0原文

我不敢相信没有人明白这一点,或者也许我只是错过了一些东西。

我有一个自定义的 DataSourceController,它负责检索数据并将其提供给其余的应用程序控件。 当然,它使用我也初始化的 sql 连接。

我的应用程序代码:

private ISQLConnection conn;
public ISQLConnection SqlConnection { get { return conn; } }

private DataSourceController dataSource;
public DataSourceController DataSource { get { return dataSource; } }

protected override void OnStartup(StartupEventArgs e) {
    //-------------------------------------------------------
    // Initialize connections
    conn = new OracleSQLConnection("connectionStringHere");

    //-------------------------------------------------------
    // Initialize controllers
    //dataSource = new DataSourceController(conn);

    base.OnStartup(e);
}

现在我想在 XAML 中创建 ObjectDataProvider,然后使用它来绑定控件中的数据:

<ObjectDataProvider ObjectType="{x:Type data:DataSourceController}" x:Key="DataSource" MethodName="GetVenues" />

问题是 DataSourceController 没有无参数构造函数,需要传入 OracleSQLConnection 对象(这是我的应用程序代码隐藏)。

这是可能吗? 或者我必须对我想要数据绑定的每个控件使用代码内 DataContext 属性?!

I cannot believe that nobody has gotten across this or perhaps I'm just missing something.

I've got a custom DataSourceController which handles retrieving data and providing it to the rest of the application controls. Naturally, it uses sql connection which I also initialize.

My App code:

private ISQLConnection conn;
public ISQLConnection SqlConnection { get { return conn; } }

private DataSourceController dataSource;
public DataSourceController DataSource { get { return dataSource; } }

protected override void OnStartup(StartupEventArgs e) {
    //-------------------------------------------------------
    // Initialize connections
    conn = new OracleSQLConnection("connectionStringHere");

    //-------------------------------------------------------
    // Initialize controllers
    //dataSource = new DataSourceController(conn);

    base.OnStartup(e);
}

Now I want to create ObjectDataProvider in XAML and then use it for binding data in controls:

<ObjectDataProvider ObjectType="{x:Type data:DataSourceController}" x:Key="DataSource" MethodName="GetVenues" />

The problem is that the DataSourceController does not have a parameterless constructor and requires an OracleSQLConnection object to be passed in (which is a public property in my App code-behind).

Is this at all possible? Or I have to resort to using in-code DataContext property for each control I want to data-bind?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

若相惜即相离 2024-07-22 12:01:09

您可以将 DataSourceController 添加为资源(如果没有无参数构造函数,则必须在代码隐藏中执行此操作)并使用 ObjectDataProvider.ObjectInstance 属性来获取 DataSourceController 实例,然后您可以对其执行方法:

private ISQLConnection conn;
public ISQLConnection SqlConnection { get { return conn; } }

private DataSourceController dataSource;
public DataSourceController DataSource { get { return dataSource; } }

protected override void OnStartup(StartupEventArgs e) {
   //-------------------------------------------------------
   // Initialize connections
   conn = new OracleSQLConnection("connectionStringHere");

   //-------------------------------------------------------
   // Initialize controllers
   dataSource = new DataSourceController(conn);
   this.Resources.Add("myDataController", dataSource);
   base.OnStartup(e);
}

ObjectInstance 可以获取资源:

<ObjectDataProvider ObjectInstance="{StaticResource myDataController}" x:Key="DataSource" MethodName="GetVenues" />

如果您的 ObjectDataProvider 位于 App.xaml 中,可能会在 之前对其进行解析,则可能会出现问题添加了 DataSourceController 资源,在这种情况下,您会收到错误,因为“myDataController”资源尚不存在。

You can add the DataSourceController as a resource (you'll have to do this from code-behind if you don't have a parameterless constructor) and use the ObjectDataProvider.ObjectInstance property to get the DataSourceController instance, then you can execute methods on it:

private ISQLConnection conn;
public ISQLConnection SqlConnection { get { return conn; } }

private DataSourceController dataSource;
public DataSourceController DataSource { get { return dataSource; } }

protected override void OnStartup(StartupEventArgs e) {
   //-------------------------------------------------------
   // Initialize connections
   conn = new OracleSQLConnection("connectionStringHere");

   //-------------------------------------------------------
   // Initialize controllers
   dataSource = new DataSourceController(conn);
   this.Resources.Add("myDataController", dataSource);
   base.OnStartup(e);
}

ObjectInstance can take a resource:

<ObjectDataProvider ObjectInstance="{StaticResource myDataController}" x:Key="DataSource" MethodName="GetVenues" />

There might be issues if your ObjectDataProvider is in App.xaml where it could be parsed before the DataSourceController resource is added, in which case you'd get an error because the "myDataController" resources wouldn't exist yet.

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