我之前为自定义控件创建了几个自定义绑定,但由于这种情况是针对按钮的window.resources样式(相反,控制模板),我不知道在哪里开始为后面的代码。我将在哪里创建viewmodel,它将继承或引用什么?
XAML:
<Style x:Key="UnifiedButtonStyle" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="{Binding Margin}"/>
<Setter Property="Background" Value="#FFDDDDDD"/>
<Setter Property="BorderBrush" Value="#FF707070"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Content" Value="Button"/>
<Setter Property="Width" Value="75"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid
x:Name="ButtonGrid"
Background="{TemplateBinding Background}"
OpacityMask="{TemplateBinding OpacityMask}">
<Border
x:Name="ButtonBorder"
BorderBrush="{TemplateBinding BorderBrush}"
OpacityMask="{TemplateBinding OpacityMask}"
BorderThickness="{TemplateBinding BorderThickness}">
<Label
x:Name="ButtonLabel"
Foreground="{TemplateBinding Foreground}"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Label>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonGrid" Property="Background" Value="{Binding HoverColorBackground}"/>
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{Binding HoverColorBorder}"/>
<Setter TargetName="ButtonLabel" Property="Foreground" Value="{Binding HoverColorForeground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Hovercolor setter是这里的关键
答案 0 :(得分:1)
创建一个viewmodel来自定义wpf按钮颜色是一种错误的方法。按钮颜色方案是严格属于视图的东西。此外,许多按钮意味着许多视图模型实例,因为每个按钮可能都是唯一的 - 这样一个简单设置的代码太多了。
Button类没有足够的依赖项属性来设置代表HoverColorBackground / HoverColorBorder / HoverColorForeground的颜色。替代方法是创建派生的Button类(当DP具有某种复杂类型和/或具有关联逻辑时的方式)或使用附加属性。我写了a tip, which coveres the second approach。
简短版
创建附加的DP
public static class Alt
{
#region Background
public static readonly DependencyProperty BackgroundProperty =
DependencyProperty.RegisterAttached("Background", typeof(Brush),
typeof(Alt), new PropertyMetadata(null));
public static Brush GetBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(Alt.BackgroundProperty);
}
public static void SetBackground(DependencyObject obj, Brush value)
{
obj.SetValue(Alt.BackgroundProperty, value);
}
#endregion
}
为该属性设置自定义值
<Button Content="Blue" Foreground="White" Margin="5"
Background="Blue" ui:Alt.Background="DarkBlue"/>
确保模板知道如何使用该属性
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonGrid"
Property="Background"
Value="{Binding Path=(ui:Alt.Background),
RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
适用于任何控件。许多DP可以任意组合混合。