如何识别ObjectDataProvider的数据源 [英] How to identify data source for an ObjectDataProvider

查看:92
本文介绍了如何识别ObjectDataProvider的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模块中有一个名为CalMonths的枚举。 我使用以下XAML来使用该枚举:

 xmlns:self =" clr-namespace:HIPPO" 

.....

< ObjectDataProvider
x:Key =" CalMonths"
MethodName =" GetValues"
ObjectType =" {x:Type sys:Enum}">
< ObjectDataProvider.MethodParameters>
< x:Type TypeName =" self:CalMonths" />
< /ObjectDataProvider.MethodParameters>
< / ObjectDataProvider

但我得到编译时错误"名称"CalMonths"在命名空间"clr-namespace:HIPPO"中不存在。


这样做的正确方法是什么?

解决方案

你好SezMe ,


对于ObjectDataProvider,它允许我们创建类的实例并调用各种方法,同时允许为其参数赋值。我做了一个样本,你可以看看:


首先,我创建一个类名Calculator类,有两种方法。

公共类计算器
{
公共双加(双一,双二)
{
返回一+二;
}

public string Add(string arg1,string arg2)
{
int x = 0;
int y = 0;
if(int.TryParse(arg1,out x)&& int.TryParse(arg2,out y))
{
return this.Add(x,y).ToString( );
}
其他
{
返回"输入错误!" ;;
}
}
}

然后我引用这个类,并将此类用于ObjectDataProvide ObjectType,methodName为Add,Parameter为string 。该方法返回一个字符串是daoasource。

< Window.Resources> 
< ObjectDataProvider
x:Key =" odp"
MethodName =" Add"
ObjectType =" {x:Type local:Calculator}">
< ObjectDataProvider.MethodParameters>
< system:String> 6< / system:String>
< system:String> 8< / system:String>
< /ObjectDataProvider.MethodParameters>
< / ObjectDataProvider>
< /Window.Resources>


< StackPanel> 
< TextBox
x:Name =" textBox1"
Margin =" 5"
Text =" {Binding Source = {StaticResource odp},Path = MethodParameters [0],BindsDirectlyToSource = true,UpdateSourceTrigger = PropertyChanged}" />
< TextBox
x:Name =" textBox2"
Margin =" 5"
Text =" {Binding Source = {StaticResource odp},Path = MethodParameters [1],BindsDirectlyToSource = true,UpdateSourceTrigger = PropertyChanged}" />
< TextBox
x:Name =" textBox3"
Margin =" 5"
Text =" {Binding Source = {StaticResource odp},Mode = OneWay}" />
< / StackPanel>

现在你可以从odp获得数据源。



最诚挚的问候,


Cherry





I have an Enum named CalMonths in a module.  I use the following XAML to use that Enum:

xmlns:self="clr-namespace:HIPPO"

.....

<ObjectDataProvider
     x:Key="CalMonths"
     MethodName="GetValues"
     ObjectType="{x:Type sys:Enum}">
     <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="self:CalMonths" />
     </ObjectDataProvider.MethodParameters>
</ObjectDataProvider

but I get a compile-time error "The name "CalMonths" does not exist in the namespace "clr-namespace:HIPPO".

What is the proper way to do this?

解决方案

Hi SezMe,

For ObjectDataProvider, it allows us to creates an instance of the class and makes calls to various methods also allowing to give values to its parameters. I do one sample , you can take a look:

Firstly, I create one class name Calculator class, there are two method.

 public class Calculator
    {
        public double Add(double one, double two)
        {
            return one + two;
        }

        public string Add(string arg1, string arg2)
        {
            int x = 0;
            int y = 0;
            if (int.TryParse(arg1, out x) && int.TryParse(arg2, out y))
            {
                return this.Add(x, y).ToString();
            }
            else
            {
                return "Input Error!";
            }
        }
    }

Then I quote this class, and use this class for ObjectDataProvide ObjectType, methodName is Add,Parameter is string. The method return a string is daoasource.

 <Window.Resources>
        <ObjectDataProvider
            x:Key="odp"
            MethodName="Add"
            ObjectType="{x:Type local:Calculator}">
            <ObjectDataProvider.MethodParameters>
                <system:String>6</system:String>
                <system:String>8</system:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

<StackPanel>
        <TextBox
            x:Name="textBox1"
            Margin="5"
            Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox
            x:Name="textBox2"
            Margin="5"
            Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox
            x:Name="textBox3"
            Margin="5"
            Text="{Binding Source={StaticResource odp}, Mode=OneWay}" />
    </StackPanel>

Now you can get datasource from odp.

Best Regards,

Cherry


这篇关于如何识别ObjectDataProvider的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆