Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户控件在Silverlight中可以具有隐式样式吗?_C#_Silverlight_Xaml - Fatal编程技术网

C# 用户控件在Silverlight中可以具有隐式样式吗?

C# 用户控件在Silverlight中可以具有隐式样式吗?,c#,silverlight,xaml,C#,Silverlight,Xaml,解决方案:UserControl不是实际类型,因此我无法在usercontrols上使用隐式样式。谢谢你,蒂姆。 以下隐式样式似乎没有任何作用 <Style TargetType="UserControl"> <Setter Property="FontFamily" Value="Webdings"/> <Setter Property="Foreground" Value="Red"/> </Style> 我知道样式正在加

解决方案:UserControl不是实际类型,因此我无法在usercontrols上使用隐式样式。谢谢你,蒂姆。

以下隐式样式似乎没有任何作用

<Style TargetType="UserControl">
    <Setter Property="FontFamily" Value="Webdings"/>
    <Setter Property="Foreground" Value="Red"/>
</Style>

我知道样式正在加载到Apps资源字典中,因为如果我在同一个.xaml文件中显式设置另一个样式,它就可以正常工作

例如:

<Style TargetType="Control" x:Key="BaseStyle">
    <Setter Property="FontFamily" Value="Webdings"/>
</Style>

如果我将Style=“{StaticResource BaseStyle}”放入标记中,效果很好。 谢谢
-Shane

您需要指定控件的实际类型,而不仅仅是使用UserControl。隐式样式仅适用于特定类型。因此,不是:

<Style TargetType="UserControl">    
    <Setter Property="FontFamily" Value="Webdings"/>    
    <Setter Property="Foreground" Value="Red"/>
</Style>

您可以使用:

<Style TargetType="my:MyUserControl">    
    <Setter Property="FontFamily" Value="Webdings"/>    
    <Setter Property="Foreground" Value="Red"/>
</Style>


其中,
my
被声明为您的命名空间,
MyUserControl
是您的UserControl派生控件的实际类名。

如果您将实际类型而不是
UserControl
指定为TargetType,是否有效?这是我的问题。隐式仅适用于特定类型,由于我的所有用户控件都继承自该类型,因此我无法使用隐式样式。谢谢。@Tim-你应该把你的评论升级为一个答案(尽管它很短,也许再详细一点),这样@shane就可以把它标记为答案。@slugster-打得好。我现在已经做到了。