我经常为样式化数据输入表单编写以下类似代码,但我的问题是 TextBox
和 TextBlock
似乎没有实现 BaseElementStyle
中的 Setters。通常我需要分别定义它们。
这是为什么?有办法解决吗?
我猜测这与它们通常用于其他控件模板有关(例如 TextBlock 在大多数控件中使用,而 TextBox 在 DatePickers 和 ComboBoxes 中使用)。
<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" />