Consider the following style:
<Window.Resources>
<Style x:Key="NumberButton" TargetType="Button">
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding}" FontSize="20" FontFamily="Consolas"/>
</DataTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="Click" Handler="OnClicked"/>
</Style>
</Window.Resources>
My goal here was to be able to create multiple buttons, each one representing a number 0-9. For example, here is the button for number 0:
<Button Grid.Row="3" Grid.Column="1" Style="{StaticResource NumberButton}" Content="0"/>
My understanding of how DataContext works is that if you don't explicitly set it in the XAML, it is supposedly NULL, which instructs the binding to use the parent's DataContext instead. This is transitive, so it would continue to move up each parent until it found an explicitly set DataContext to use.
I'm however confused at how my binding is able to map to the Content attribute. I know that ContentControl's default property is the Content property, but I never explicitly set the DataContext on the <Button> element. To me, this would mean that the Button's DataContext is NULL, and thus it would not be able to find the value to display.
Can someone explain how the DataContext of the Button is being referred to when I haven't set it?