简体   繁体   English

将XAML ObjectDataProvider转换为C#

[英]Converting XAML ObjectDataProvider to C#

I would like to create my ObjectDataProvider in my C# code behind rather than my XAML. 我想在我的C#代码中创建我的ObjectDataProvider,而不是我的XAML。

I was wondering how to change this XAML into equivalent C#. 我想知道如何将此XAML更改为等效的C#。 The XAML was generated by Microsoft Expression Blend 2, so the d: namespace can be ignored safely. XAML由Microsoft Expression Blend 2生成,因此可以安全地忽略d:名称空间。

<ObjectDataProvider x:Key="FooSourceDS" ObjectType="{x:Type myNS:FooSource}" d:IsDataSource="True"/>

myNS is a namespace referencing my CLR object. myNS是一个引用我的CLR对象的命名空间。

I'm getting hung up on specifying the ObjectType in C#: 我正忙着在C#中指定ObjectType:

ObjectDataProvider FooSourceDS = new ObjectDataProvider();
FooSourceDS.ObjectType = myNamespace.FooSource;

The Intellisence is correctly identifying FooSource as a 'type' which is what ObjectType is looking for isn't it? Intellisence正确地将FooSource识别为'类型',这是ObjectType正在寻找的不是吗?

这是你需要的吗?

FooSourceDS.ObjectType = typeof(myNamespace.FooSource)

The answer to your question is what @Bryan wrote, But cross check whether you want an ObjectDataProvider at the code lever or not. 你的问题的答案是@Bryan写的,但是交叉检查你是否想在代码杠杆上使用ObjectDataProvider。 ObjectDataProvider is just a XAML way of instantiating a particular Class for Binding. ObjectDataProvider只是一种实例化Binding特定类的XAML方法。 But if you wish to code in C# then you dont really require ObjectDataProvider. 但是如果你想用C#编写代码那么你真的不需要ObjectDataProvider。 Just create an Instance of the FooSource and use it. 只需创建一个FooSource实例并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM