我的应用程序是基于这个示例
我需要自己的按钮样式(没有鼠标操作动画等),所以我在app.xaml中做了这个:
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border"
CornerRadius="2" BorderThickness="1"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>我的按钮:<Button IsEnabled="true"/>
现在,如果我将按钮更改为<Button IsEnabled="false"/>,我的应用程序会在一开始崩溃,错误如下:"{DependencyProperty.UnsetValue}“不是属性"BorderBrush”的有效值。
我做错什么了?
发布于 2014-09-11 14:24:57
这和你的静态参考有关。
特别是,XAML解析在顺序上非常敏感--在解析器以上述样式到达行之前,必须确保引用了x:Key="DisabledForegroundBrush"刷子--即使上面的样式与DisabledForegroundBrush位于同一个文件中。
如果您还没有针对DisabledForegroundBrush的画笔,您可以删除在上面的代码中引用它的行(如果不需要),或者,如果您想要,可以按如下方式创建一个:
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />在那里你可以选择适当的颜色。或者,您可以在这里选择其他类型的画笔:http://msdn.microsoft.com/en-us/library/aa970904(v=vs.110).aspx。
如果您有一个您想要使用的刷子,那么如果您可以提供更多关于刷子在代码库中的位置的信息(例如,它在资源字典中吗?)在DisabledForegroundBrush刷子所在的地方,这可能有助于我找到一个实际的解决方案/确保引用刷子的最佳方法。
注意:如果不能确保首先解析DisabledForegroundBrush,另一种方法是将StaticResource更改为DynamicResource__,但除非资源的链接在运行时实际更改,否则不建议这样做(参见https://stackoverflow.com/questions/200839/whats-the-difference-between-staticresource-and-dynamicresource-in-wpf )
一个更简单的解决方案:
如果您只想对样式进行硬编码,而不是在外部引用前景刷,那么您可以更改行:
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>至:
<Setter Property="Foreground" Value="[SOME COLOR]"/>为了摆脱为您的字体创建单独的画笔对象的需要。
https://stackoverflow.com/questions/25789867
复制相似问题