Note that there are some explanatory texts on larger screens.

plurals
  1. POMultibinding ConvertBack doesn't pass the correct value to source?
    primarykey
    data
    text
    <p>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].</p> <p>It works well in Convert() Method, [Entity.X] and [Entity.Z] are correctly passed to the [Canvas.LeftProperty].</p> <p>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.</p> <p>PS:I found a similar question ,but it was not solved either.. :(</p> <p>Similiar question:<a href="http://social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/88B1134B-1DAA-4A54-94ED-BD724724D1EF" rel="nofollow">http://social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/88B1134B-1DAA-4A54-94ED-BD724724D1EF</a></p> <h2>xaml:</h2> <pre><code>&lt;Canvas&gt; &lt;Button x:Name="btnTest"&gt; &lt;Canvas&gt; </code></pre> <h2>BindingCode:</h2> <pre><code>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); } </code></pre> <h2>Converter:</h2> <pre><code>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 } } </code></pre> <h2>Entity:</h2> <pre><code>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)); } } </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload