Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binding to a dependency property
Message
From
23/02/2011 15:52:42
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
23/02/2011 13:05:56
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01501473
Message ID:
01501503
Views:
29
>>I have a textblock I am trying to bind to a dependency property. The value is a property of an object. What is the syntax to specify the specific dependency property to get the value from?
>>
>>
>>(TextBlock Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" TextTrimming="CharacterEllipsis" Padding="5 0 0 0"
>>                   Text="{TemplateBinding ctrl:MapViewDataBase.Text1}"
>>                   TextWrapping="NoWrap"
>>                   FontSize="12" FontWeight="Medium"
>>                   VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="195" /)
>>
>>public MapViewDataBase ViewData
>>    {
>>      get { return (MapViewDataBase)GetValue(ViewDataProperty); }
>>      set { SetValue(ViewDataProperty, value); }
>>    }
>>
>>    public static readonly DependencyProperty ViewDataProperty =
>>        DependencyProperty.Register("ViewData", typeof(MapViewDataBase), typeof(MapESRIWPFHint), new UIPropertyMetadata(null));
>>
>>
>>
>>The MapViewDataBase object contains the values in its properties, but the binding does not seem to specify which property it is bound to, only the type of its value. Any help appreicated.
>>
>>Tim
>
>Not enough code to go on here, but as a wild guess:
>
>Text="{TemplateBinding ctrl:MapViewDataBase.ViewData, Path=Text1}"
>
>MapViewDataBase is your class. ViewData is your dependency property, so you need to include it. A class can specify many DPs.
>
>Text1 I'm guessing is the property you want to use. I'm assuming this is all inside a control template.
>
>Edit: If the control template your creating is not for the MapViewDataBase control, I think you will also need to register attached instead of just register. If it is for the MapViewDataBase control I think you might just be able to do Text="{TemplateBinding ViewData, Path=Text1}" but I haven't tested that.

Hi John,

I haven't tried your last edit, but changing to this did work. I am not sure if changing from a TemplateBinding is a bad thing or not.
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ViewData.Text1}"
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform