使用对象数据提供者
我不敢相信没有人明白这一点,或者也许我只是错过了一些东西。
我有一个自定义的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
DataSourceController
添加为资源(如果没有无参数构造函数,则必须在代码隐藏中执行此操作)并使用 ObjectDataProvider.ObjectInstance 属性来获取DataSourceController
实例,然后您可以对其执行方法:ObjectInstance
可以获取资源:如果您的
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 theDataSourceController
instance, then you can execute methods on it:ObjectInstance
can take a resource:There might be issues if your
ObjectDataProvider
is in App.xaml where it could be parsed before theDataSourceController
resource is added, in which case you'd get an error because the "myDataController
" resources wouldn't exist yet.