How to override template style in xaml

时间:2016-07-11 19:50:49

标签: xaml silverlight custom-controls

In our silverlight app, we have a container view which contains a ResourceDictionary tag to a styles.xaml. Any of the views that the container calls, inherit the same styles.

The problem we have is that we have a custom control made up of a button + textbox + popup. When we use this custom control in any view, the control will work, but the appearance in incorrect because it inherits from the container style.

The custom control doesn't look right when used in these view. But if we remove the Style.xaml, the custom control looks fine.

We need to use this style template.

Is there a way to tell our custom control to not inherit from the container styles template?

1 个答案:

答案 0 :(得分:0)

让我们说你有一个控件

<UserControl x:Name="MyControl">
  <TextBlock Text="Small Text" FontSize="8" />
</UserControl>

你很高兴。

但是用户将其放入此容器

<Grid>
 <Grid.Resources>
   <Style TargetType="TextBlock">
     <Setter Property="Foreground" Value="Red" />
   </Style
 </Grid.Resources>
 <MyControl />
</Grid>

现在文本块将变为红色和小。这是您面临的问题类型。

如果您不希望控件的用户覆盖此样式,则需要明确设置文本块的颜色,例如

<UserControl x:Name="MyControl">
  <TextBlock Text="Small Text" Foreground="Black" FontSize="8" />
</UserControl>

ps对不起,如果我的属性名称有误,那我已经有一段时间了,因为我使用了WPF / Silverlight