这些简单的样式突然失效了。直到今天他们都工作得很好。
<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Padding" Value="5,1" />
</Style>
<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Padding" Value="2,0" />
</Style>
这两个都显示 BasedOn
属性上的错误。
The resource "{x:Type TextBlock}" could not be resolved.
The resource "{x:Type TextBox}" could not be resolved.
如果我复制并粘贴其中一种样式,则粘贴的样式不会出现错误。
<Style x:Key="noErrorOnThisStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Padding" Value="5,1" />
</Style>
<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Padding" Value="5,1" />
</Style>
<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Padding" Value="2,0" />
</Style>
最佳答案
事实上,在您的情况下,不需要 BasedOn 属性。就这么写
<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="5,1" />
</Style>
<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="2,0" />
</Style>
未设置BasedOn时,BaseOn指向TargetType属性指定的类型的默认样式。
问候
克劳德
关于.net - Style BasedOn 突然不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473963/