=_=这其实是一个低级错误。。。可是最后还是我同事帮我发现的,很是惭愧,决定写一篇随笔用于记录。。。
引用的地方是这样写的:
请注意他这边是Template="***";
那么你资源文件中正确的写法应该是:
<!--textbox样式--> <ControlTemplate x:Key="TextBoxTemplate1" TargetType="{x:Type TextBox}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="5"> <ScrollViewer x:Name="PART_ContentHost"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" TargetName="Bd" Value="#FF4176D3"/> <Setter Property="BorderThickness" TargetName="Bd" Value="1"/> <Setter Property="Foreground" Value="#FF4176D3"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
如果你把这个控件模板包在style里面就是这样写:
<Style x:Key="TextBoxTemplate1" TargetType="{x:Type TextBox}"> <Setter Property="Template"> <Setter.Value> <!--textbox样式--> <ControlTemplate> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="5"> <ScrollViewer x:Name="PART_ContentHost"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" TargetName="Bd" Value="#FF4176D3"/> <Setter Property="BorderThickness" TargetName="Bd" Value="1"/> <Setter Property="Foreground" Value="#FF4176D3"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
就会报错。。。原因很简单你的样式文件是个style,而你引用的地方是个Template,层级关系不一样了。。。所以报错