-2
我之前为自定义控件创建了一些自定义绑定,但由于此例是针对按钮的window.resources样式的,(而非控件模板),我不知道后面的代码从哪里开始。我将在哪里创建视图模型,以及它会继承或引用它?为window.resources风格创建自定义绑定属性
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制定者在这里
我试过你的方法,但是每当我在datacontext中设置其中一个属性时,就会抛出一个stackoverflowexception。我没有丝毫的线索为什么。当属性没有设置,但只是简单的默认datacontext,它不会抛出错误,我没有看到任何递归循环 – Varscott11
编辑:Nevermind上述评论。我忘了给主类添加一个静态导致错误,你猜它是个例外。像这个答案很多。 – Varscott11