WPF,这个资源引用为什么报错?解决方法
WPF,这个资源引用为什么报错?
一个放在App.xaml中的应用程序资源:
一个自定义控件CustomControl1:
在自定义控件CustomControl1的样式中,Background引用应用程序资源aaa,结果报错:

为什么不能引用呢?
------解决思路----------------------
提示中的 “其它信息” 说的很明显 UnsetValue 不是有效值 什么类型的
------解决思路----------------------
自定义控件中有<Application>吗?
------解决思路----------------------

Background 只能引用非Static 的颜色呗
你直接拿 Blue放到Backgound里不就可以了么。
------解决思路----------------------
自定义控件应该不能引用application中的资源吧, 尝试用dynamicresource来引用application中的资源。或者将资源直接跟自定义控件样式放在一起。
一个放在App.xaml中的应用程序资源:
<Application x:Class="WPF1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush x:Key="aaa" Color="Blue"/>
</Application.Resources>
</Application>
一个自定义控件CustomControl1:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF1">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Background" Value="{StaticResource aaa}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在自定义控件CustomControl1的样式中,Background引用应用程序资源aaa,结果报错:
为什么不能引用呢?
------解决思路----------------------
提示中的 “其它信息” 说的很明显 UnsetValue 不是有效值 什么类型的
------解决思路----------------------
自定义控件中有<Application>吗?
------解决思路----------------------
Background 只能引用非Static 的颜色呗
你直接拿 Blue放到Backgound里不就可以了么。
------解决思路----------------------
自定义控件应该不能引用application中的资源吧, 尝试用dynamicresource来引用application中的资源。或者将资源直接跟自定义控件样式放在一起。