locked
Multibinding - pass original values on ConvertBack RRS feed

  • Question

  • Is there a way to pass original values on ConvertBack of Multibinding ?

    this is my code on XAML:

                <Slider Maximum="1024" TickFrequency="1" IsSnapToTickEnabled="True">
                    <Slider.Value>                    
                        <MultiBinding Converter="{StaticResource sliderTrackPositionConverter}">
                            <Binding Path="Position"  />                        
                            <Binding Mode="OneWay" Path="Length" />                                                
                        </MultiBinding>                    
                    </Slider.Value>
                </Slider>

    On the ConvertBack method of sliderTrackPositionConverter I got only the slider value, but I need also the Length property. 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
    ... need Length !!!
    }

    Is there a way to get Length  ?

    Saturday, September 29, 2012 1:35 PM

Answers

  • Solved.

    Used the DependecyProperty injection in Slider along with Artificial  Inheritance Context approach using ' DataContextSpy.

    <Slider Maximum="1024" TickFrequency="1">
    	<Slider.Resources>
    		<local:DataContextSpy x:Key="dataContextSpy" />
    	</Slider.Resources>
    	<Slider.Value>
    		<Binding Path="Position">
    			<Binding.Converter>
    				<conv:SliderConverter Length="{Binding Source={StaticResource dataContextSpy}, Path=DataContext.Length, Mode=OneWay}" />                 
    			</Binding.Converter>
    			<Binding.ConverterParameter>
    				<sys:Int32>1024</sys:Int32>
    			</Binding.ConverterParameter>
    		</Binding>
    	</Slider.Value>
    
    </Slider>

    class SliderConverter : DependencyObject, IValueConverter
    {
    
    	public long Length
    	{
    		get { return (long)GetValue(LengthProperty); }
    		set { SetValue(LengthProperty, value); }
    	}
    
    	public static readonly DependencyProperty LengthProperty =
    		DependencyProperty.Register("Length", typeof(long), typeof(SliderConverter), new UIPropertyMetadata(null));
    
    	public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
    	{
    		...
    	}
    	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    	{
    		...
    	}
    }

    • Marked as answer by Zenon66 Monday, October 1, 2012 8:23 AM
    Monday, October 1, 2012 8:16 AM

All replies

  • Hi,

    You should probably do reverse of what you did in convert method.

    In Multibinding converter, Convert Method can only get multiple objects you should implement your logic to convert the multiple objects into single object. similarly Convert Back methods can return multiple values but we must implement our own logic to convert back the single object to Multiple objects.

    Hope this helps...


    srithar

    Saturday, September 29, 2012 4:09 PM
  • I need both object either in convert and convertback as parameter.

    In the convert method slider position is computed by the two binded variables (slider moves by a timer).

    In the convertback I wish to return the Position variable, but for this purpose I need Slider value and  Length variable too

    Sunday, September 30, 2012 1:28 AM
  • Solved.

    Used the DependecyProperty injection in Slider along with Artificial  Inheritance Context approach using ' DataContextSpy.

    <Slider Maximum="1024" TickFrequency="1">
    	<Slider.Resources>
    		<local:DataContextSpy x:Key="dataContextSpy" />
    	</Slider.Resources>
    	<Slider.Value>
    		<Binding Path="Position">
    			<Binding.Converter>
    				<conv:SliderConverter Length="{Binding Source={StaticResource dataContextSpy}, Path=DataContext.Length, Mode=OneWay}" />                 
    			</Binding.Converter>
    			<Binding.ConverterParameter>
    				<sys:Int32>1024</sys:Int32>
    			</Binding.ConverterParameter>
    		</Binding>
    	</Slider.Value>
    
    </Slider>

    class SliderConverter : DependencyObject, IValueConverter
    {
    
    	public long Length
    	{
    		get { return (long)GetValue(LengthProperty); }
    		set { SetValue(LengthProperty, value); }
    	}
    
    	public static readonly DependencyProperty LengthProperty =
    		DependencyProperty.Register("Length", typeof(long), typeof(SliderConverter), new UIPropertyMetadata(null));
    
    	public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
    	{
    		...
    	}
    	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    	{
    		...
    	}
    }

    • Marked as answer by Zenon66 Monday, October 1, 2012 8:23 AM
    Monday, October 1, 2012 8:16 AM