数据绑定在WPF的方法 [英] Databinding to a method in WPF

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

问题描述

我有麻烦数据绑定一个 TextBox.Text 属性设置为对象的方法。这个想法是允许用户在文本框文件名写,然后有一个的TextBlock 输出文件的扩展名。

 类GetFileInfo 
{
公共字符串GetFileExtension(字符串文件名)
{
返回Path.GetExtension(文件名);
}
}

下面是我的XAML:

 < Window.Resources> 
< ObjectDataProvider的X:键=getFileInfo方法名=GetFileExtension对象类型={X:键入地方:GetFileInfo}>
< ObjectDataProvider.MethodParameters>
< SYS:字符串> abc.text< / SYS:字符串>
< /ObjectDataProvider.MethodParameters>
< / ObjectDataProvider的>
< /Window.Resources>

<&StackPanel的GT;
<文本框名称=textBox1的>
< TextBox.Text>
<绑定源={StaticResource的getFileInfo}路径=MethodParameters [0]BindsDirectlyToSource =真UpdateSourceTrigger =的PropertyChanged/>
< /TextBox.Text>
< /文本框>
< TextBlock的名称=textBlock1文本={绑定源= {StaticResource的getFileInfo}}/>
< / StackPanel的>

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





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




下面是给出的错误




System.Windows.Data错误:8:不能从目标保存值回试图在运行时设置其他文本时去调试源。 BindingExpression:路径= MethodParameters [0];的DataItem ='的ObjectDataProvider'(的hashCode = 2207369);目标元素是'文本框'(名称='textBox1的'); target属性是文本(类型'字符串')的ArgumentException:System.ArgumentException:类型的对象'MS.Internal.Data.PropertyPathWorker + IListIndexerArg无法转换为类型System.Int32。
在System.RuntimeType.TryChangeType(对象的值,粘结剂粘合,CultureInfo的文化,布尔needsSpecialCast)
在System.RuntimeType.CheckValue(对象的值,粘结剂粘合,CultureInfo的文化的BindingFlags invokeAttr)
在System.Reflection.MethodBase.CheckArguments(对象[]参数,粘结剂粘结,的BindingFlags invokeAttr,CultureInfo的文化,签名SIG)
在System.Reflection.RuntimeMethodInfo.Invoke(obj对象,的BindingFlags invokeAttr,粘结剂粘合,对象[ ]参数,CultureInfo的文化,布尔skipVisibilityChecks在System.Reflection.RuntimeMethodInfo.Invoke(obj对象,的BindingFlags invokeAttr,粘结剂粘合)
,对象[]参数,CultureInfo的文化)在System.Reflection.RuntimePropertyInfo
。的SetValue(obj对象,对象的值,的BindingFlags invokeAttr,粘结剂粘合,对象[]索引,CultureInfo的文化)
在MS.Internal.Data.PropertyPathWorker.SetValue(对象项目,对象的值)
在MS。 Internal.Data.ClrBindingWorker.UpdateValue(对象的值)
在System.Windows.Data.BindingExpression.UpdateSource(对象的值)



解决方案

好吧,看起来像这样运行WPF 4.0时,如可在注释的这里




Woops,我是有点仓促..这个例子完美的作品只要你编译它的(在VS 2010中)的3.5框架。
但是,如果你把它转换为WPF 4.0项目上的ObjectDataProvider方法WeightOnPlanet是nolonger上调用ODP2当您编辑文本框。
我试图找到任何绑定或ObjectDataProvider的任何新的属性 - 但没有制定了迄今为止...




当在3.5编译,它工作在这里很好。


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);
    }
}

Here is my 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>

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:

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

Here is the error given by de debugger when trying to set other text at run-time:

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)'

解决方案

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

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 …

When compiling in 3.5, it works fine here.

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

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