CSHTML5 has now become OpenSilver!

We are happy to announce that CSHTML5 has been significantly improved and rebranded to 'OpenSilver', which stands for 'Open-source reimplementation of Silverlight'. It is fully backward compatible and it can be downloaded from Upgrading from CSHTML5 to OpenSilver is very easy.
Read the FAQ




The "RelativeSource" keyword is used in XAML to specify the source of a binding.

The following RelativeSource modes are currently supported:



Use "Self" if you want to bind one property of an element to another property on the same element. With this mode, the element on which the binding is applied will be the source of the binding.

Here is an example:

<TextBlock Text="{Binding Width, RelativeSource={RelativeSource Mode=Self}}" x:Name="TextBlock1" Width="200"/>

In this example, the TextBlock will display the text "200", because the Text property is bound to the Width property.

Please note that this is NOT the same as:

<TextBlock Text="{Binding Width}" x:Name="TextBlock1" Width="200"/>

The line above will NOT work because it will be looking for a property "Width" on the DataContext. By "DataContext" we mean the content of the property "TextBlock1.DataContext". The DataContext is an inherited property, which means that "TextBlock1.DataContext" will have the same value as the DataContext property of the parent of the TextBlock.

An alternative way to achieve the same result as "Selft" is to use "ElementName". With "ElementName" you can tell it to use the element itself as the source of the binding. Here is an example:

<TextBlock Text="{Binding Width, ElementName=TextBlock1}" x:Name="TextBlock1" Width="200"/>



Use "TemplatedParent" if you are inside a ControlTemplate and you want to bind a property of an element to a property of the templated control.

For example, consider the following XAML code:

        <Style x:Key="MyRoundButtonStyle" TargetType="Button">

            <!-- Set the default value for some properties of the button: -->
            <Setter Property="Background" Value="Gray"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Padding" Value="12"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>

            <!-- Define how the button will be rendered: -->
            <Setter Property="Template">
                    <ControlTemplate TargetType="Button">
                                Fill="{Binding Background, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
    <Button x:Name="MyTemplatedButton"
            Style="{StaticResource MyRoundButtonStyle}"

In the code above, we create a round button by creating a new style using a ControlTemplate. The "Fill" property of the ellipse has a binding to the "Background" property of the templated button.

Please note that the two following lines are strictly equivalent:

<Ellipse Fill="{Binding Background, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>


<Ellipse Fill="{TemplateBinding Background}"/>

The main advantage of the "Binding" syntax compared to the "TemplateBinding" syntax is that the former allows you to specify a "Converter" to convert the bound value via a class that inherits from IValueConverter.


Contact Us

Please click here for contact information.