Multibinding ConvertBack doesn't pass the correct value to source?
Asked Answered
P

1

5

There is a very strange problem which is confusing me a lot.Just like the code below,I had created a [Button] and multibound its [Canvas.LeftProperty] to [Entity.X] and [Entity.Z].The [Entity] class has implemented the [INotifyPropertyChaned].

It works well in Convert() Method, [Entity.X] and [Entity.Z] are correctly passed to the [Canvas.LeftProperty].

But the problem is:when I changed the [Button]'s location with Canvas.SetLeft() method,the ConvertBack() methods was fired,but the correct value was not passed to the [Entity],the [value] in [Entity.X]'s set section seemed to be the old one all the time.

PS:I found a similar question ,but it was not solved either.. :(

Similiar question:http://social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/88B1134B-1DAA-4A54-94ED-BD724724D1EF

xaml:

<Canvas>
  <Button x:Name="btnTest">
<Canvas>

BindingCode:

private void Binding()
{
 var enity=DataContext as Entity;
 var multiBinding=new MutiBinding();
 multiBinding.Mode=BindingMode.TwoWay;
 multiBinding.Converter=new LocationConverter();
 multiBinding.Bindings.Add(new Binding("X"));
 multiBinding.Bindings.Add(new Binding("Z"));
 btnTest.SetBinding(Canvas.LeftProperty,multiBinding);
}

Converter:

public class LocationConverter: IMultiValueConverter
{
  public object Convert(object[] values, TypetargetType,object parameter, CultureInfo culture)
  {
    return (double)values[0]*(double)values[1];
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  {
    return new object[]{ (double)value,Binding.DoNoting};//!!value here is correct
  }
}

Entity:

public class Entity:INotifyPropertyChanged
{
  private double x=0d;
  private double z=0d;

  public double X
  {
    get{ return x;}
    set{ 
         x=value;//!!value here is not correctly passed
         CallPropertyChanged("X");}
       } 

  public double Z
  {
    get{ return z;}
    set{ 
         z=value;//!!value here is not correctly passed
         CallPropertyChanged("Z");}
       } 
  } 

  public event PropertyChangedEventHandler PropertyChanged;

  private void CallPropertyChanged(String info)
  {
     if(PropertyChanged!=null)
        PropertyChanged(this,new PropertyChangedEventArgs(info));
  }
}

Paratyphoid answered 29/5, 2013 at 4:30 Comment(2)
can I see your whole Property please?Exudation
@dennis schütz ,Thank u for your time.The code above is updated now.Paratyphoid
C
9

You need to specify the Binding mode on each Binding within your MultiBinding that you want to use in your ConvertBack method. So for the code you posted above the following change to your 'Binding code' should fix your issue:

private void Binding()
{
 var enity=DataContext as Entity;
 var multiBinding=new MutiBinding();
 multiBinding.Mode=BindingMode.TwoWay;
 multiBinding.Converter=new LocationConverter();
 multiBinding.Bindings.Add(new Binding("X"){Mode = BindingMode.TwoWay});
 multiBinding.Bindings.Add(new Binding("Z"));
 btnTest.SetBinding(Canvas.LeftProperty,multiBinding);
}
Christelchristen answered 2/6, 2013 at 20:26 Comment(1)
Thanks a lot,It does work!But why must the BindingMode on each Binding be specified since the MultiBinding has already had one?Could you please give me some more advices?Thank u!Paratyphoid

© 2022 - 2024 — McMap. All rights reserved.