-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