Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
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:
XML
<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>



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

which I was doing this way:
HTML
<TextBox Name="textBoxSpot" DataContext="{Binding Source={StaticResource Spot}}" Text="{Binding Path=MID}" >

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:
XML
<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" />


which is working fine too.

So I tried the same thing with my TextBox, many ways without success, and finaly it looks like this:
XML
<TextBox.Text>
   <MultiBinding Converter="{StaticResource SpotMultiConverter}">
         <Binding Source="{StaticResource Spot}" Mode="OneWay"  />
         <Binding ElementName="textBoxSpot" Path="Text" Mode="OneWayToSource" />
   </MultiBinding>
</TextBox.Text>


The SpotMultiConverter looks like this:

C#
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,
            };

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!
Posted
Updated 12-Jan-12 21:58pm
v2

Could we see your MultiBinding Converter code?
 
Share this answer
 
Comments
William2011 13-Jan-12 4:01am    
I updated the question with the Converter.
It is based on the example I found there: http://stackoverflow.com/questions/848946/is-it-possible-to-bind-wpf-combobox-selectedvalue-to-multiple-objectdataprovider
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,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900