多重绑定和WPF [英] multibinding and WPF

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

问题描述

你好,

使用Visual Studio c#2010和WPF,我有一个TextBox,可以通过公开为ObjectDataProvider的方法来设置,也可以由最终用户手动设置.
此外,此值将用作公开为另一个ObjectDataProvider的另一个方法的输入.

我已经在不同的组件上实现了这种行为,但是我无法将所有内容组合在一起.

我的方法如下:

Hello,

using visual studio c# 2010 and WPF, I have a TextBox that will be either set by a method exposed as an ObjectDataProvider, or manually set by the end user.
Moreover, this value will be used as an input for another method exposed as another ObjectDataProvider.

I have implemented this kind of behavior on different components, but I have trouble combining everything together.

my method looks like this:

<ObjectDataProvider x:Key="UISpotProvider" ObjectType="{x:Type uilayercommon:UISpotProvider}"/>
        <ObjectDataProvider x:Key="Spot"

          ObjectInstance="{StaticResource UISpotProvider}"

          MethodName="getSpot">
            <ObjectDataProvider.MethodParameters>
                <system:String>Parameter1</system:String>
                <system:DateTime>2011-12-12</system:DateTime>
                <system:String>EUR/USD</system:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>




它返回一个UISpot类型的对象,其成员String MID我希望显示在我的TextBox中.

我这样做的方式是:




it returns an object of type UISpot whose member String MID I wish to display in my TextBox.

which I was doing this way:

<TextBox Name="textBoxSpot" DataContext="{Binding Source={StaticResource Spot}}" Text="{Binding Path=MID}" >


这很好.

现在,我要做的就是使用文本框"textBoxSpot"中的值(可能已被最终用户覆盖)作为各种方法或组件的输入.
我是在另一个使用MultiBinding的组件中完成此操作的,我得到了这样的内容:


this works fine.

now what I want to do, is to use the value in my textbox "textBoxSpot", which may have been overwritten by the end user, as an input to various methods or component.
I did that in another component using the MultiBinding, and I got something like this:

<MultiBinding Converter="{StaticResource CcyPairMultiConverter}" Mode="OneWayToSource">
    <Binding ElementName="comboBoxInstrument" Path="SelectedItem" Mode="OneWayToSource" />
    <Binding Source="{StaticResource Spot}" Path="MethodParameters[2]" BindsDirectlyToSource="True" Mode="OneWayToSource" />
    <Binding Source="{StaticResource PriceUnit}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource" />



效果也很好.

所以我用TextBox尝试了同样的事情,很多方法都没有成功,最后看起来像这样:



which is working fine too.

So I tried the same thing with my TextBox, many ways without success, and finaly it looks like this:

<TextBox.Text>
   <MultiBinding Converter="{StaticResource SpotMultiConverter}">
         <Binding Source="{StaticResource Spot}" Mode="OneWay"  />
         <Binding ElementName="textBoxSpot" Path="Text" Mode="OneWayToSource" />
   </MultiBinding>
</TextBox.Text>



SpotMultiConverter看起来像这样:



The SpotMultiConverter looks like this:

public class UISpotMultiConverter : IMultiValueConverter
    {

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return values[0];
            return ((UISpot)values[0]).MID;
            //throw new NotImplementedException("implement me UICurrencyPairConfigurationMultiConverter.Convert");
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return new object[] { };

            }
            return new object[]
            {
                value,
            };


当我手动设置文本框时,我看到了通过ConvertBack传递的值,并且当通过Spot提供者进行设置时,我看到了UISpot的value [0],但是我只能在文本框中显示它.我尝试使用Path将其作为字符串,对象返回.目前我不知道该怎么办.

任何帮助将不胜感激,

干杯!


When I set the textbox manually, I see the value going through the ConvertBack, and when it is set via the Spot provider, I see the UISpot in value[0] but I just can get to display it in the textbox. I tried returning it as a String, as an Object, using Path. At this point I don''t know what to do.

Any help would be much appreciated,

Cheers!

推荐答案

我们能看到您的MultiBinding Converter代码吗?
Could we see your MultiBinding Converter code?


显示问题是由于类型错误double MID vs字符串TextBox.Text
我本来希望wpf能够自动处理这种转换,所以有些惊讶.
在转换器中使用toString和double.Parse进行了修复.

现在,关于第二个问题,我无法管理由方法设置的绑定或由用户手动设置的绑定.因为在我的Convert方法中,我将返回value [0](该方法)或value [1]作为手动输入.并且无法确定我应该在运行时使用哪一个.
这太麻烦了,所以我删除了此绑定并直接在c#中使用该方法,在另一个控件上用SelectionChanged方法调用了该方法.
那么我只需要按照其他示例将TextBox.Text绑定到divers ObjectDataProvider MethodParameter.

不知道在xaml中是否直接有解决方案,但是针对5行代码花费了太多时间.

问候
the display problem was due to a type error double MID vs String TextBox.Text
a little surprised I would have expected wpf to handle this kind of conversion automatically.
fixed with toString and double.Parse in the Converter.

Now regarding the second issue, I was not able to manage the binding to be either set by the method, or set manualy by the user. since in my Convert method, I was returning either the value[0] (the method) or value[1] the manual input. and was not able to determine which one I was suppose to use at runtime.
this is too much of a hassle, so I removed this binding and use the method directly in c#, called with a SelectionChanged method on the other control.
then I just have to Bind the TextBox.Text to divers ObjectDataProvider MethodParameter as per other example.

Not sure if there was a solution directly in xaml, but it was taking too much time against 5 lines of code.

Regards,


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

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