C# 使用BasedOn更改控件属性
我有一个带有一些按钮的用户控件。我需要更改按钮的背景颜色,但保留所有其他属性和颜色,如鼠标悬停事件 我使用了下面的代码,我希望它能够基于C# 使用BasedOn更改控件属性,c#,wpf,xaml,C#,Wpf,Xaml,我有一个带有一些按钮的用户控件。我需要更改按钮的背景颜色,但保留所有其他属性和颜色,如鼠标悬停事件 我使用了下面的代码,我希望它能够基于{StaticResource{x:Type Button}}}定义UniqueButton1,但是更改background属性来定义我自己的颜色 <UserControl x:Class="Project.Detail" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pr
{StaticResource{x:Type Button}}}
定义UniqueButton1
,但是更改background属性来定义我自己的颜色
<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>
按钮2是使用
<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"/>
<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是使用
<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"/>
<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。我想克隆按钮3属性,只将背景更改为粉红色。将你的
BaseButton
从x:Type
更改为命名按钮,然后在其他BasedOn
上设置它的name
,这应该可以做到。你这么说gUniqueButton1
样式是否修改默认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 default,而我的usercontrol继承了主题按钮吗?Ie B2是expressionblend,B3是主题。如何具体说明在{x:Type Button}
中使用的默认按钮?