【发布时间】:2016-07-23 06:42:36
【问题描述】:
我制作了那个自定义按钮,让我可以更改背景颜色 normal-hover-pressed-disabled
我为所有这些属性创建了一个新属性,除了正常值之外,因为它已经存在但我不知道如何为其设置默认值
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ProPharmacyManagerW.Controls
{
class IconButton : Button
{
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(IconButton), new PropertyMetadata(null));
public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(IconButton), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(IconButton), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty HoverColorProperty = DependencyProperty.Register("ColorHover", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.LightGray));
public static readonly DependencyProperty PressedColorProperty = DependencyProperty.Register("ColorPressed", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.Gray));
public static readonly DependencyProperty DisabledColorProperty = DependencyProperty.Register("ColorDisabled", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.Gray));
static IconButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(IconButton), new FrameworkPropertyMetadata(typeof(IconButton)));
}
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public double ImageHeight
{
get { return (double)GetValue(ImageHeightProperty); }
set { SetValue(ImageHeightProperty, value); }
}
public double ImageWidth
{
get { return (double)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
}
public SolidColorBrush ColorHover
{
get { return (SolidColorBrush)GetValue(HoverColorProperty); }
set { SetValue(HoverColorProperty, value); }
}
public SolidColorBrush ColorPressed
{
get { return (SolidColorBrush)GetValue(PressedColorProperty); }
set { SetValue(PressedColorProperty, value); }
}
public SolidColorBrush ColorDisabled
{
get { return (SolidColorBrush)GetValue(DisabledColorProperty); }
set { SetValue(DisabledColorProperty, value); }
}
}
}
主题
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ProPharmacyManagerW"
xmlns:f="clr-namespace:ProPharmacyManagerW.Controls"
xmlns:vm="clr-namespace:ProPharmacyManagerW.ViewModel">
<vm:VisibilityConvertor x:Key="VisibilityConvertor"/>
<Style TargetType="{x:Type f:IconButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type f:IconButton}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image x:Name="Buttonicon"
Margin="10 0"
Source="{TemplateBinding Image}"
Width="{TemplateBinding ImageWidth}"
Height="{TemplateBinding ImageHeight}"
Visibility="{TemplateBinding Image,Converter={StaticResource VisibilityConvertor}}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="0"/>
<TextBlock x:Name="contentText"
Text="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="Center"
Grid.Column="1"/>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{Binding ColorHover, RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{Binding ColorPressed, RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{Binding ColorDisabled, RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
正常背景颜色的默认值 - 没有触发器(过度 - 按下 - 禁用) - 是无,我想将其更改为白色或能够在设计时从控件属性更改它的东西
【问题讨论】:
-
你对除了正常的默认值是什么意思?
-
正常背景颜色的默认值 - 没有触发器(过度 - 按下 - 禁用) - 是无,我想将其更改为白色或能够在设计时从控件属性更改它的东西
-
不理会背景属性。为每个状态添加属性,覆盖默认样式,并在模板中引用您的新属性。