【问题标题】:Changing control properties using BasedOn使用 BasedOn 更改控件属性
【发布时间】:2012-08-30 11:54:50
【问题描述】:

我有一个带有一些按钮的 UserControl。我需要更改按钮的背景颜色,但保留所有其他属性和颜色,例如鼠标悬停事件

我使用了以下代码,我希望它可以基于 {StaticResource {x:Type Button}}" 定义 UniqueButton1,但更改背景属性以定义我自己的颜色

<UserControl x:Class="Project.Detail"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="800" BorderBrush="#FF5380E7" BorderThickness="0,0,0,1" xmlns:my="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
    <UserControl.Resources>
        <Style  x:Key="UniqueButton1" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"  >
            <Setter Property="Background">
                <Setter.Value>
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="#FFF896CE" Offset="0" />
                    <GradientStop Color="#FFF788C7" Offset="0.5" />
                    <GradientStop Color="#FFF570BB" Offset="0.5" />
                     <GradientStop Color="#FFF353AE" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid Height="30" Width="800">
        <Button Height="30" HorizontalAlignment="Left" Margin="360,0,0,0" Name="button2" VerticalAlignment="Top" Width="50" Click="b_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" FontStretch="SemiCondensed" />
        <Button Style="{StaticResource UniqueButton1}" Height="30" HorizontalAlignment="Left" Margin="423,0,0,0" Name="button1" VerticalAlignment="Top" Width="50" Click="b_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" FontStretch="SemiCondensed" />
    </Grid>
</UserControl>

它有点工作,因为我以我需要的方式获得背景颜色,但我在&lt;UserControl.Resources&gt; 中定义的UniqueButton1 不是基于没有应用样式更改的其他默认按钮2。例如,背景、颜色、动画速度等都不同

也许是因为我也在应用一个主题?

我的问题是 1. 如何定义基于默认button2 并应用了主题的资源? 要么 2. 如何在应用了主题的情况下获取 button2 的所有属性,以便构建自己的样式?

有人建议我可能需要建立自己的风格,这很好。但我需要匹配默认主题按钮的行为,除了背景属性 - 如果我看不到它是如何组成的,这似乎有点麻烦。

编辑:澄清事情

这两张图片显示了 3 个按钮,您可以看到 button3 的鼠标悬停是橙色的,而 button2 的鼠标悬停是蓝色的。你看不到的是动画速度也不同。本质上,我希望我的按钮在静止状态下看起来像 button1,在鼠标悬停时看起来像 button3,同时保留 button3 动画速度和其他属性。 Button2 是我正在使用的,没有进一步更改属性。

Button3 是在没有资源和 XAML 的情况下生成的

<Button Height="30" HorizontalAlignment="Left" Margin="360,0,0,0" VerticalAlignment="Top" Width="50" Click="button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" FontStretch="SemiCondensed"/>

Button2 是使用

生成的
<UserControl.Resources>
        <Style  x:Key="TEST1" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"  >
        </Style>
</UserControl.Resources>

<Button Style="{StaticResource TEST1}" Height="30" HorizontalAlignment="Left" Margin="360,0,0,0" VerticalAlignment="Top" Width="50" Click="button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" FontStretch="SemiCondensed"/>

Button1 是使用

生成的
    <UserControl.Resources>
        <Style  x:Key="UniqueButton1" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"  >
            <Setter Property="Background">
                <Setter.Value>
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="#FFF896CE" Offset="0" />
                    <GradientStop Color="#FFF788C7" Offset="0.5" />
                    <GradientStop Color="#FFF570BB" Offset="0.5" />
                     <GradientStop Color="#FFF353AE" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

<Button Style="{StaticResource UniqueButton1}" Height="30" HorizontalAlignment="Left" Margin="360,0,0,0" VerticalAlignment="Top" Width="50" Click="button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" FontStretch="SemiCondensed" />

所以显然按钮 1 和 2 不是基于我想要的按钮 3。我想克隆 button3 属性并且只将背景更改为粉红色。

【问题讨论】:

  • 所以你是说UniqueButton1样式修改了默认WPF按钮的背景,而不是修改了你的主题按钮样式的背景?
  • @Jay - 我更新了我的 Q 以尝试让事情变得更清晰
  • 基本上,您的按钮有一个主题,并且您想扩展按钮 1 和 2 的默认样式?我不明白。你还提到你想继承 button1 的所有状态和 button3 上的鼠标悬停。
  • B3 是一个主题按钮,我想保留 B3 的所有属性,除了背景而不是灰色(B3 右下图)我希望它像 B1 一样粉红色。 B2 仅在我尝试克隆 B3 时显示它不是 B3。因此,B1 是粉红色的,但所有属性都不是主题,如 B3 - B1 和 B2 在鼠标悬停时变为蓝色而不是橙色。如何克隆 B3 主题并将背景设为粉红色,或者如何从 B3 中提取所有属性(鼠标悬停、动画速度等)以重建样式以匹配 B3(粉红色背景除外)?
  • 另一个想法 - 是不是因为 BasedOn="{StaticResource {x:Type Button}} 默认使用 Expressionblend Button 而我的用户控件继承了主题按钮?即 B2 是 expressionblend & B3 是主题。如何具体说明{x:Type Button} 中使用了哪个默认按钮?

标签: c# wpf xaml


【解决方案1】:

将您的 BaseButtonx:Type 更改为已命名的,然后将其设置为 name 其他 BasedOn,这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多