我的wpf应用中有一个按钮,如下所示:
<Button x:Name="button" Content="" HorizontalAlignment="Left" Margin="402,10,0,0" VerticalAlignment="Top" Width="26" Height="28" BorderBrush="{x:Null}" Grid.Column="1" Foreground="White">
<Button.Background>
<ImageBrush ImageSource="cls_blk_btn.png" Stretch="None"/>
</Button.Background>
</Button>
现在你可以看到按钮有一个背景图像,是否有可能保留图像的背景颜色?
我要做的是在鼠标输入时更改按钮的背景颜色(保持图像不变):
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Color" To="#FF484A4D" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ParallelTimeline >
<ColorAnimation Storyboard.TargetProperty="Color" To="#FF57626C" Duration="0:0:0.1" />
</ParallelTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
问题是,由于背景是图像,我在尝试更改颜色时会出现异常...有任何帮助保持图像不变并更改backColor ???
这就是我刚刚尝试过的事情
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="resources\c_ml.bmp" Stretch="Fill"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="Resources\c_mo.bmp" Stretch="Fill"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
但它为我提供了&#39; System.Windows.Baml2006.TypeConverterMarkupExtension&#39;在第45行引出了一个例外。&#39; 。抛出异常的行是:
<Style TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="resources\c_ml.png" Stretch="Fill"/>
</Setter.Value>
</Setter>
<Style.Triggers>
知道怎么解决吗?
我修复了我的第二个错误,但是有一个新问题
发生了第二个错误,因为我没有在我的项目中包含图片。包括之后,我遇到了一个新问题。现在,鼠标悬停时,按钮变为白色,我的意思是它改变了背景颜色而不是改变图像......出了什么问题?
答案 0 :(得分:1)
你必须把图像放在背景中吗?如果你只想在按钮中显示带有背景的图像,那么将图像放在Button.Content中会更容易,并为背景设置动画,例如:
<Button x:Name="button" HorizontalAlignment="Left" Margin="402,10,0,0" VerticalAlignment="Top" Width="260" Height="280" BorderBrush="{x:Null}" Foreground="White">
<Button.Content>
<Image Source="image.png" Stretch="None" />
</Button.Content>
<Button.Background>
<SolidColorBrush Color="#FF57626C" />
</Button.Background>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="#FF484A4D" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ParallelTimeline >
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="#FF57626C" Duration="0:0:0.1" />
</ParallelTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
答案 1 :(得分:0)
固定IT MYSELF
def mergedict(A,B):
r1 = []
k = {}
for (k,v), (k2,v2) in zip(A.items(), B.items()):
if k=='photos':
r1.append(A[k]['photo'])
elif k2=='photos':
r1.append(B[k2]['photo'])
k['photos']=r1[0]
return k
C=mergedict(A,B)
print(C)