0
我想在鼠標移過時更改按鈕的文本顏色(前景)。我通過編輯下面的代碼如何更改WPF中按鈕的前景色?
<Style x:Key="ButtonStyle_black" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
<Setter Property="Background" Value="{DynamicResource GrayBrush10}"/>
<Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper"/>
<Setter Property="Controls:ControlsHelper.CornerRadius" Value="3"/>
<Setter Property="FontFamily" Value="{DynamicResource DefaultFont}"/>
<Setter Property="FontSize" Value="{DynamicResource UpperCaseContentFontSize}"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="MinHeight" Value="25"/>
<Setter Property="Padding" Value="5,6"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="Background" Background="{TemplateBinding Background}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{x:Null}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<Border x:Name="DisabledVisualElement" Background="{DynamicResource ControlsDisabledBrush}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" IsHitTestVisible="False" Opacity="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<Controls:ContentControlEx x:Name="PART_ContentPresenter" ContentCharacterCasing="{Binding (Controls:ControlsHelper.ContentCharacterCasing), RelativeSource={RelativeSource TemplatedParent}}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ButtonMouseOverBorderBrush}"/>
<Setter Property="BorderThickness" TargetName="Border" Value="2"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" TargetName="DisabledVisualElement" Value="0.7"/>
<Setter Property="Opacity" TargetName="PART_ContentPresenter" Value="0.3"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但編輯該模板前景色變化不工作已成功地改變背景顏色。
SRY我沒有具體的,是我不好,我想對事件改變前景鼠標。謝謝。 – user3086871
@ user3086871檢查更新後的答案,如果答案有幫助,則標記爲答案 – Sajeetharan