简体   繁体   中英

WPF binding to property of non-simple property of other class

A little bit can't figure out how to use WPF binding in this case:

Assume, we have an object Car with non-simple property of type CarInfo:

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); }
    }

}

Also assume, Car is an ui element and it has the Car.xaml, something simple:

<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>

So, I wanted this TextBlock, in my Car.xaml, to represent the property "MaxSpeed" of my CarInfo class, which is actually a property of my Car class. How can I do this?

Thank you in advance, appreciate any help! :)

It depends upon what is assigned to the DataCOntext of the UI element representing the Car - you need to specify a binding path relative to that. In this case I would suggest you start with this:

<TextBlock Text="{Binding Path=Info.MaxSpeed}" />

this is assuming that a Car object has been assigned to the DataContext of the Car UI element.

Note that your properties don't have to be dependency properties - you can also bind to normal properties (depending on what you are doing).

Edit

It seems you are looking to use element binding, so you should be able to achieve what you want by using either the TemplatedParent or an ancestor as your relative source. See this previous SO answer for an example. Your binding should look something like this:

<TextBlock Text="{Binding Path=Info.MaxSpeed, RelativeSource={RelativeSource TemplatedParent}}" />

This will take you back to your templated parent control (the Car), then travel down the Info property of the UI element to the MaxSpeed property on its contents.

As I said in my comment, you are making this very messy by having your UI element so closely match your data element and then assigning your data object to a relatively non standard property on the UI element. You might have your reasons, but XAML and WPF don't need to be that complicated.

<TextBlock Text="{Binding Path=Info.MaxSpeed}" />

That code works well for me:

<TextBlock Text="{Binding Path=Info.MaxSpeed, RelativeSource={RelativeSource Mode=TemplatedParent}}" />

and usage:

Car.Info = new CarInfo { MaxSpeed = 100.0 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM