1

In looking through the MS Reference source they show the constructors for RelativeSource. Below is the one used when you specify FindAncestor as the mode.

public RelativeSource(RelativeSourceMode mode, Type ancestorType, int ancestorLevel)
        {
            InitializeMode(mode);
            AncestorType = ancestorType;
            AncestorLevel = ancestorLevel;
        }

Now I don't see anything that allows optional arguments here.

And yet the below XAML works fine even though I haven't specified AncestorLevel.

    <TextBlock Height="50" Text="{Binding RelativeSource={RelativeSource 
    FindAncestor, AncestorType={x:Type Window}}, Path=Height}"/>

Why can I leave the AncestorLevel argument out?

0

3 Answers 3

1

The string "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Height}" is not calling the constructor for RelativeSource. It is calling the Binding XAML Markup extensions and the RelativeSource XAML Markup Extension. These extensions have their own syntax and provide their own defaults.

1

In XAML include Markup Extensions all objects are instantiated per parameterless default constructor. All properties are set after creation. The constructor with parameters is not meant for XAML

0

Check the XAML RelativeSource MarkupExtension:

intLevel - Optional for FindAncestor mode. An ancestor level (evaluated towards the parent direction in the logical tree).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.