在这种情况下,有点想不通如何使用 WPF 绑定:
假设,我们有一个 CarInfo 类型的非简单属性的对象 Car:
public class CarInfo : DependencyObject
{
public static readonly DependencyProperty MaxSpeedProperty =
DependencyProperty.Register("MaxSpeed", typeof (double), typeof (CarInfo), new PropertyMetadata(0.0));
public double MaxSpeed
{
get { return (double) GetValue(MaxSpeedProperty); }
set { SetValue(MaxSpeedProperty, value); }
}
}
public class Car : DependencyObject
{
public static readonly DependencyProperty InfoProperty =
DependencyProperty.Register("Info", typeof (CarInfo), typeof (Car), new PropertyMetadata(null));
public CarInfo Info
{
get { return (CarInfo) GetValue(InfoProperty); }
set { SetValue(InfoProperty, value); }
}
}
还假设 Car 是一个 ui 元素并且它具有 Car.xaml,这很简单:
<Style TargetType="assembly:Car">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="assembly:Car">
<Grid >
!--> <TextBlock Text="{Binding Path=MaxSpeed}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
所以,我希望我的 Car.xaml 中的这个 TextBlock 代表我的 CarInfo 类的属性“MaxSpeed”,它实际上是我的 Car 类的属性。我怎样才能做到这一点?
提前感谢您,感谢您的帮助!:)