数据绑定到WPF中的一个方法 [英] Databinding to a method in WPF

查看:248
本文介绍了数据绑定到WPF中的一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个 TextBox.Text 属性数据绑定到对象的方法上有困难。这个想法是允许用户在$ code> TextBox 中写入一个文件名,然后输入一个 TextBlock 输出该文件的扩展名。

I am having trouble databinding a TextBox.Text property to a object's method. The idea is allowing the user to write in a TextBox a file name and then have a TextBlock output that file's extension.

class GetFileInfo
{
    public string GetFileExtension(string fileName)
    {
        return Path.GetExtension(fileName);
    }
}

这是我的XAML:

<Window.Resources>
    <ObjectDataProvider x:Key="getFileInfo" MethodName="GetFileExtension" ObjectType="{x:Type local:GetFileInfo}">
        <ObjectDataProvider.MethodParameters>
            <sys:String>abc.text</sys:String>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<StackPanel>
    <TextBox Name="textBox1">
        <TextBox.Text>
            <Binding Source="{StaticResource getFileInfo}" Path="MethodParameters[0]" BindsDirectlyToSource="True" UpdateSourceTrigger="PropertyChanged" />
        </TextBox.Text>
    </TextBox>
    <TextBlock Name="textBlock1" Text="{Binding Source={StaticResource getFileInfo}}"/>
</StackPanel>

由于某种原因,它没有做任何事情。 Anyknow可以指出可能是什么原因?
这是我在设计师和运行应用程序时看到的:

For some reason it is not doing anything. Anyknow could point out what may be the reasons? Here is what I see on the designer and when I run the application:

文字http://img64.imageshack.us/img64/6922/f0rki14feo3oa3groxm5kgm.png

这里是什么发生在运行时尝试设置其他文本时:

And here is what happens when I try setting other text at run-time:

alt文本http://img684.imageshack.us/img684/1057/b52pnq1dm4i23qivizvpaqq.png
这是在调试器尝试在运行时设置其他文本时给出的错误:

alt text http://img684.imageshack.us/img684/1057/b52pnq1dm4i23qivizvpaqq.png Here is the error given by de debugger when trying to set other text at run-time:


System.Windows.Data错误:8:无法将值从目标还原到源。 BindingExpression:Path = MethodParameters [0]; DataItem ='ObjectDataProvider'(HashCode = 2207369);目标元素为'TextBox'(Name ='textBox1');目标属性为'Text'(类型'String')ArgumentException:'System.ArgumentException:类型为'MS.Internal.Data.PropertyPathWorker + IListIndexerArg'的对象不能转换为类型'System.Int32'。
在System.RuntimeType.TryChangeType(Object value,Binder binder,CultureInfo culture,Boolean needsSpecialCast)
在System.RuntimeType.CheckValue(对象值,Binder binder,CultureInfo文化,BindingFlags invokeAttr)
在System.Reflection.MethodBase.CheckArguments(Object []参数,Binder binder,BindingFlags invokeAttr,CultureInfo文化,签名sig)
在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object [ ]参数,CultureInfo文化,Boolean skipVisibilityChecks)
在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)
在System.Reflection.RuntimePropertyInfo。 SetValue(Object obj,Object value,BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo culture)
在MS.Internal.Data.PropertyPathWorker.SetValue(对象项目,对象值)
在MS。内部的a.ClrBindingWorker.UpdateValue(Object value)
在System.Windows.Data.BindingExpression.UpdateSource(Object value)'

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=MethodParameters[0]; DataItem='ObjectDataProvider' (HashCode=2207369); target element is 'TextBox' (Name='textBox1'); target property is 'Text' (type 'String') ArgumentException:'System.ArgumentException: Object of type 'MS.Internal.Data.PropertyPathWorker+IListIndexerArg' cannot be converted to type 'System.Int32'. at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value) at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value) at System.Windows.Data.BindingExpression.UpdateSource(Object value)'


推荐答案

好的,在运行WPF 4.0时似乎是一个错误,在评论中可以看到这里

Ok, seems like this a bug when running WPF 4.0, as can be seen in comments here.


Woops,我有点仓促..这个例子很好用很长时间你编译为3.5框架(在VS 2010)。
但是如果将其转换为WPF 4.0项目,则在编辑文本框时,ObjectDataProvider上的方法WeightOnPlanet在odp2上被调用。
我试图在Binding或ObjectDataProvider上找到任何新的属性 - 但是目前还没有任何工作...

Woops, I was a bit hasty .. the example works perfectly as long you compile it for the 3.5 framework (in VS 2010). But if you convert it to WPF 4.0 project the method WeightOnPlanet on ObjectDataProvider is nolonger invoked on odp2 when you edit the textbox. I have tried to find any new Attributes on either the Binding or the ObjectDataProvider – but nothing has worked out so far …

在3.5中编译时,这里工作正常。

When compiling in 3.5, it works fine here.

这篇关于数据绑定到WPF中的一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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