【问题标题】:while Inherit style in WPF it affect parent style而在 WPF 中继承样式会影响父样式
【发布时间】:2015-09-07 18:01:07
【问题描述】:

在 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>

现在我需要为下面的其他地方覆盖 customcontrol 边框,

<Style TargetType="local:CustomControl" BasedOn="{StaticResource  {x:Type local:CustomControl}}">
    <Setter Property="BorderThickness" Value="1" />
</Style>

我的问题是当我使用上面的代码时,它会覆盖第一次编写的代码。我的代码是否正确。

注意:基本样式只写目标类型。我需要在不影响基本代码的情况下在其他地方覆盖该控件边框。

有可能吗?请帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: c# xaml styles wpf-controls


    【解决方案1】:

    如果您声明 Style 没有 x:Key,它将覆盖该控件的默认样式。

    <Style TargetType="local:CustomControl">
    

    因此,上面的代码将影响整个应用程序(或范围内)的所有 CustomControl 元素。

    如果您想要覆盖基本样式,您可以将Style 指定为x:Key,如下所示:

    <Style TargetType="local:CustomControl" x:Key="MyAwesomeStyle">
    

    创建控件时,您必须引用Style。这是一个例子:

    <local:CustomControl Style="{DynamicResource MyAwesomeStyle}" ... />
    

    【讨论】:

    • 但我需要一些地方的基本风格和派生风格。
    • @gobi007 希望您在最小的地方使用派生样式而不是基本样式。所以在派生样式中添加键并使用自定义控件的键,这些都需要派生样式。编译器不能仅仅理解哪个自定义控件需要基本样式,哪个自定义控件需要派生样式。
    • @Karuppasamy 我需要为所有地方应用基本样式。在某些地方,我需要更改一些基本样式的属性,例如边框厚度。所有其他样式属性必须应用到所有地方。我的问题的主要目的是覆盖特定的属性(边框厚度)值。
    【解决方案2】:

    偶然我看到了一些对解决上述问题很有用的例子。在您的示例中,使用了自己的自定义控件,在我的示例中 - 一个按钮。

            <Grid>
                <Button Style="{StaticResource AddButtonStyle}" Tag="Add" Click="addClick" />
            </Grid>
    

    AddButtonStyle 的代码:

           <Style x:Key="AddButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
              <Setter Property="Content" Value="&#x2705;"/>
           </Style>
    

    AddButtonStyle 基于 AppBarButtonStyle。下面是它的代码。

            <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 声明样式,并且不应为继承样式中的 Content(在您的示例中为 BorderThickness)属性设置任何值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多