而在 WPF 中继承样式会影响父样式
while Inherit style in WPF it affect parent style
在 WPF 中,我有如下所示的控件样式,
<Style TargetType="local:CustomControl">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="Padding" Value="3,0,3,0" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
现在我需要覆盖其他地方的自定义控件边框,如下所示,
<Style TargetType="local:CustomControl" BasedOn="{StaticResource {x:Type local:CustomControl}}">
<Setter Property="BorderThickness" Value="1" />
</Style>
我的问题是当我使用上面的代码时它覆盖了第一个编写的代码。我的代码是否正确。
注意:base style只写target type。我需要在不影响基本代码的情况下在其他地方覆盖该控制边界。
可能吗?请帮我解决这个问题。
提前致谢。
如果您声明 Style
而没有 x:Key
,它将覆盖该控件的默认样式。
<Style TargetType="local:CustomControl">
所以上面的代码将影响整个应用程序(或范围内)的所有 CustomControl
个元素。
如果您不想覆盖基本样式,您可以给您的Style
一个x:Key
,像这样:
<Style TargetType="local:CustomControl" x:Key="MyAwesomeStyle">
创建控件时,必须引用 Style
。这是一个例子:
<local:CustomControl Style="{DynamicResource MyAwesomeStyle}" ... />
无意间看到了一些例子,可以解决上述问题。在您的示例中,使用了自己的自定义控件,在我的示例中 - 一个按钮。
<Grid>
<Button Style="{StaticResource AddButtonStyle}" Tag="Add" Click="addClick" />
</Grid>
AddButtonStyle 代码:
<Style x:Key="AddButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="Content" Value="✅"/>
</Style>
基于 AppBarButtonStyle 的 AddButtonStyle。下面的代码。
<Style x:Key="AppBarButtonStyle" TargetType="Button">
<Setter Property="MinWidth" Value="40" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="88" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI Symbol" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">. . .
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在此示例基础上,您必须声明带有 x:Key 的样式,并且不应在继承样式中为内容(在您的示例 BorderThickness 中)属性 设置任何值。
在 WPF 中,我有如下所示的控件样式,
<Style TargetType="local:CustomControl">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="Padding" Value="3,0,3,0" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
现在我需要覆盖其他地方的自定义控件边框,如下所示,
<Style TargetType="local:CustomControl" BasedOn="{StaticResource {x:Type local:CustomControl}}">
<Setter Property="BorderThickness" Value="1" />
</Style>
我的问题是当我使用上面的代码时它覆盖了第一个编写的代码。我的代码是否正确。
注意:base style只写target type。我需要在不影响基本代码的情况下在其他地方覆盖该控制边界。
可能吗?请帮我解决这个问题。
提前致谢。
如果您声明 Style
而没有 x:Key
,它将覆盖该控件的默认样式。
<Style TargetType="local:CustomControl">
所以上面的代码将影响整个应用程序(或范围内)的所有 CustomControl
个元素。
如果您不想覆盖基本样式,您可以给您的Style
一个x:Key
,像这样:
<Style TargetType="local:CustomControl" x:Key="MyAwesomeStyle">
创建控件时,必须引用 Style
。这是一个例子:
<local:CustomControl Style="{DynamicResource MyAwesomeStyle}" ... />
无意间看到了一些例子,可以解决上述问题。在您的示例中,使用了自己的自定义控件,在我的示例中 - 一个按钮。
<Grid>
<Button Style="{StaticResource AddButtonStyle}" Tag="Add" Click="addClick" />
</Grid>
AddButtonStyle 代码:
<Style x:Key="AddButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="Content" Value="✅"/>
</Style>
基于 AppBarButtonStyle 的 AddButtonStyle。下面的代码。
<Style x:Key="AppBarButtonStyle" TargetType="Button">
<Setter Property="MinWidth" Value="40" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="88" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI Symbol" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">. . .
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在此示例基础上,您必须声明带有 x:Key 的样式,并且不应在继承样式中为内容(在您的示例 BorderThickness 中)属性 设置任何值。