英文:
Changing the brush color of the button content
问题
在你的 MyView.xaml 中,你尝试更改按钮的鼠标悬停时的图像颜色。你已经尝试过两种方法,但都没有奏效。看起来问题在于如何更改图像颜色。
首先,确保你在 MyDictionary.xaml 中定义了 crossOnMouseHoverDrawingImage
,并使用不同的 Brush
颜色。
然后,在 MySettings 类的构造函数中确保初始化了 CrossOnMouseHoverIcon
,类似于你对 CrossIcon
的初始化。
最后,在你的样式中,你需要将鼠标悬停时的 ContentSource
设置为 CrossOnMouseHoverIcon
,而不是尝试更改前景颜色。样式应如下所示:
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="border" Value="Red"/>
<Setter Property="ContentPresenter.ContentSource" TargetName="contentPresenter" Value="{Binding UISettings.CrossOnMouseHoverIcon, UpdateSourceTrigger=PropertyChanged}"/>
</Trigger>
</ControlTemplate.Triggers>
这应该可以在鼠标悬停时更改按钮图像的颜色。如果问题仍然存在,确保你没有其他影响按钮外观的样式或触发器。
英文:
In one of my views I have an WPF button that looks like below:
MyView.xaml:
<Button Grid.Column="1"
Style="{StaticResource HoverOnCloseButton}"
Background="Transparent"
BorderBrush="Transparent"
Command="{Binding CancelBtnCommand}">
<Button.Content>
<Image Width="10"
Height="10"
MinWidth="10"
MinHeight="10"
Stretch="Uniform"
UseLayoutRounding="True"
SnapsToDevicePixels="True"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding MySettings.CrossIcon, UpdateSourceTrigger=PropertyChanged}"/>
</Button.Content>
</Button>
The button content is an image which is loaded from a property "CrossIcon" defined and initialized within a class "MySettings" that implements INotifyPropertyChanged:
public class MySettings: INotifyPropertyChanged
{
public ImageSource CrossIcon { get; private set; }
public ResourceDictionary ResourceDict { get; set; }
public MySettings()
{
InitializeDictionary();
this.CrossIcon = ResourceDict["crossDrawingImage"] as DrawingImage;
}
private void InitializeDictionary()
{
String url = String.Format("pack://application:,,,/myApp;component/Resources/Themes/Dark/MyDictionary.xaml");
// Load resource dictionary
ResourceDict = new ResourceDictionary
{
Source = new Uri(url, UriKind.RelativeOrAbsolute)
};
}
}
The MyDictionary.xaml defines the crossDrawingImage as below:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib">
<DrawingImage x:Key="crossDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="#FF000000" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</ResourceDictionary>
Now, what I am trying to do is to change the Brush (#FF000000) of the DrawingImage from my view MyView.xaml using the Style bound to the button through a trigger set on IsMouseOver:
<Style x:Key="HoverOnCloseButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Background="{TemplateBinding Panel.Background}"
BorderBrush="Transparent"
Name="border"
SnapsToDevicePixels="True">
<ContentPresenter Name="contentPresenter"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="border" Value="Red"/>
<Setter Property="TextElement.Foreground"
TargetName="contentPresenter">
<Setter.Value>
<SolidColorBrush>#FFFFFF</SolidColorBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The setter below is not working:
<Setter Property="TextElement.Foreground"
TargetName="contentPresenter">
<Setter.Value>
<SolidColorBrush>#FFFFFF</SolidColorBrush>
</Setter.Value>
</Setter>
So how can I change the brush color of the drawingImage when the mouse is over the button?
ATTEMPT #2:
As explained here, it looks like there is no way to do this, the only possible solution seems to replace the entire DrawingImage by another one, so I have done the following by applying the following changes:
MyDictionary.xaml:
I create the same DrawingImage but with different Bush color.
<DrawingImage x:Key="crossOnMouseHoverDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="#FFFFFFFF" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</ResourceDictionary>
In MySettings class i create a new property and I initialize in the constructor in the following way:
public ImageSource CrossOnMouseHoverIcon { get; private set; }
public MySettings()
{
InitializeDictionary();
this.CrossIcon = ResourceDict["crossDrawingImage"] as DrawingImage;
this.CrossOnMouseHoverIcon = ResourceDict["crossOnMouseHoverDrawingImage"] as DrawingImage;
}
Finally, I modify the Style replacing the trigger for IsMouseOver (for the rest I keep the same):
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="border" Value="Red"/>
<Setter Property="ContentPresenter.ContentSource"
TargetName="contentPresenter"
Value="{Binding UISettings.CrossOnMouseHoverIcon, UpdateSourceTrigger=PropertyChanged}"/>
</Trigger>
</ControlTemplate.Triggers>
But still not working, it does nothing when I hover on the mouse. What am I doing wrong? for sure I am setting the setter wrong, but I am not able to see what is failing.
答案1
得分: 1
或许是因为您将DrawingImage类型分配给ContentSource,但应该是Image。尝试以下按钮的实现:
<Button Width="50" Height="50" x:Name="btn" BorderBrush="Transparent">
<Button.Content>
<Image Width="10" Height="10" MinWidth="10" MinHeight="10" Stretch="Uniform" UseLayoutRounding="True" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="HighQuality">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{StaticResource blackCrossDrawingImage}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
<Setter Property="Source" Value="{StaticResource redCrossDrawingImage}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Button.Content>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Background="{TemplateBinding Panel.Background}" BorderBrush="Transparent" Name="border" SnapsToDevicePixels="True">
<ContentPresenter Name="contentPresenter" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
以及两个资源:
<DrawingImage x:Key="redCrossDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="Red" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z">
</GeometryDrawing>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
<DrawingImage x:Key="blackCrossDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="Black" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z">
</GeometryDrawing>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
英文:
Maybe because you assign a type of DrawingImage to ContentSource but it should be Image. Try the following implementation of a button
<Button Width="50" Height="50" x:Name="btn"
BorderBrush="Transparent">
<Button.Content>
<Image Width="10"
Height="10"
MinWidth="10"
MinHeight="10"
Stretch="Uniform"
UseLayoutRounding="True"
SnapsToDevicePixels="True"
RenderOptions.BitmapScalingMode="HighQuality">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{StaticResource blackCrossDrawingImage}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
<Setter Property="Source" Value="{StaticResource redCrossDrawingImage}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Button.Content>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Background="{TemplateBinding Panel.Background}"
BorderBrush="Transparent"
Name="border"
SnapsToDevicePixels="True">
<ContentPresenter Name="contentPresenter"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
and two resources
<DrawingImage x:Key="redCrossDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="Red" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z">
</GeometryDrawing>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
<DrawingImage x:Key="blackCrossDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V10 H10 V0 H0 Z">
<GeometryDrawing Brush="Black" Geometry="F0 M10,10z M0,0z M4.33959,5L0,0.660409 0.660409,0 5,4.33959 9.33959,0 10,0.660409 5.66041,5 10,9.33959 9.33959,10 5,5.66041 0.660409,10 0,9.33959 4.33959,5z">
</GeometryDrawing>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论