【发布时间】:2013-05-24 05:15:55
【问题描述】:
有一个非常奇怪的问题让我很困惑。就像下面的代码一样,我创建了一个 [Button] 并将其 [Canvas.LeftProperty] 绑定到 [Entity.X] 和 [Entity.Z]。 [Entity] 类已实现 [INotifyPropertyChaned]。
在 Convert() 方法中运行良好,[Entity.X] 和 [Entity.Z] 正确传递给 [Canvas.LeftProperty]。
但问题是:当我使用 Canvas.SetLeft() 方法更改 [Button] 的位置时,ConvertBack() 方法被触发,但没有将正确的值传递给 [Entity],[value] ] 在 [Entity.X] 的 set 部分似乎一直是旧的。
PS:我发现了一个类似的问题,但也没有解决.. :(
类似问题:http://social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/88B1134B-1DAA-4A54-94ED-BD724724D1EF
xaml:
<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 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
}
}
实体:
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));
}
}
【问题讨论】:
-
我可以看看你的整个财产吗?
-
@dennis schütz,感谢您抽出宝贵时间。上面的代码现已更新。
标签: wpf multibinding