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].
<Canvas><Button x:Name="btnTest"><Canvas>
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); }
public class Entity:INotifyPropertyChanged { private double x=0d; private double z=0d; public double X { get{ ...} set{ x=value;....} } public double Z { get{ ...} set{ z=value;....} } //INotifyPropertyChanged Members... }
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}; } }
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.
Anyone can help?Thank you for your time!
PS:I found a similar question ,but it was not solved either.. :(