0

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! :)

Alexander Knoth
  • 137
  • 1
  • 13

3 Answers3

2

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.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Ok, saying the truth I've been known how to solve this using DataContext. But I expected that I can do this without DataContext, because actually if I'm using DataContext I don't really need to create CarInfo:Info property. So, my simple code works well if I'm writting Car.DataContext = new CarInfo { MaxSpeed = 100}. But I wanted that code will be works as well: Car.Info = new CarInfo { MaxSpeed = 100}. – Alexander Knoth Oct 02 '12 at 10:00
  • @AlexanderKnoth You need to be clear with your question... if you don't want to use the DataContext then what do you want to use? Element binding? It really seems like you are getting yourself into a bit of a mess, and it's not made any easier with your UI matching your data items so exactly. – slugster Oct 02 '12 at 10:35
0
<TextBlock Text="{Binding Path=Info.MaxSpeed}" />
Danilo Vulović
  • 2,983
  • 20
  • 31
0

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 };
Alexander Knoth
  • 137
  • 1
  • 13