考虑以下样式:
<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>
我的目标是创建多个按钮,每个按钮代表一个数字0-9。例如,这里是数字0的按钮:
<Button Grid.Row="3" Grid.Column="1" Style="{StaticResource NumberButton}" Content="0"/>
我的理解是,如果您在XAML中没有明确设置DataContext,则它会被视为NULL,并指示绑定使用父级的DataContext。这是传递性的,因此它将继续向上移动每个父级,直到找到明确设置的DataContext为止。
然而,我对如何将绑定映射到Content属性感到困惑。我知道ContentControl的默认属性是Content属性,但我从未在