Die Bindung an eine relativesource in der code-behind

In meinem UserControl, ich habe folgenden code in meiner XAML -

<TextBlock Grid.Row="2" Text="{Binding Path=StartTime,
                               RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorLevel=1, AncestorType=Window}}" />

Dieser wird einfach der Wert einer Eigenschaft vom übergeordneten Fenster und es funktioniert Super.

Wie kann ich dies in den code hinter?

Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
                                      typeof(Window), 1);
b.Path = "StartTime";

myProperty = b.value;//obviously there is no b.value but this
                     //is what I'm trying to achieve.

//BindingOperations.SetBinding(StartTime, StartTimeProperty, b);
//This does not work sadly, it can't convert string to DependancyObject

InformationsquelleAutor Dave | 2013-11-05

Schreibe einen Kommentar