1
我曾嘗試以下...按鈕的控件模板的ContentPresenter的文字塊的前景不改變
- 設置按鈕的TextBlock.Foreground。
- 設置contentpresenter的TextBlock.Foreground。
- 設置IsMouseOver觸發器,如圖所示。
- 設置沒有目標名稱的IsMouseOver觸發器(假設它擊中按鈕)。
- 無處不在,我已經嘗試TextBlock.Foreground,我已經嘗試過TextElement.Foreground。
我錯過了什麼!?我假設我有一些小小的疏忽。 (這不是我的代碼,但它現在是我的責任:\
此外,請注意這一事實,使用此樣式的地方,按鈕的命令和內容綁定到mvvm樣式視圖模型。
<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" SnapsToDevicePixels="true">
<Border.Background>
<SolidColorBrush Color="{StaticResource Color2}"/>
</Border.Background>
<ContentPresenter x:Name="contentPresenter" Focusable="False" TextElement.Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" TargetName="border" Value="{StaticResource ButtonIsFocusedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsDefaulted" Value="true"/>
<!---HERE IS THE PROBLEM (note: the background works fine)--->
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource ButtonHoverOverBackgroundBrush}"/>
<Setter Property="TextBlock.Foreground" TargetName="contentPresenter" Value="White"/>
</Trigger>
<!---HERE IS THE PROBLEM--->
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="#FF8B7A54"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.33"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>