首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Silverlight按钮背景ImageBrush

Silverlight按钮背景ImageBrush
EN

Stack Overflow用户
提问于 2012-08-17 17:01:53
回答 1查看 1.3K关注 0票数 0

我有一张图片

http://savepic.su/2317454.htm

但是当我尝试将按钮的背景设置为

代码语言:javascript
复制
<UserControl x:Class="System.Windows.Controls.UserControl" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Name="plus_button" Width="30" Height="25">
    <Button.Background>
        <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="plus_unhover.jpg"></ImageBrush>
    </Button.Background>
</Button>
</UserControl>  

我有过

http://savepic.su/2309262.htm

所有浏览器中的那些图像...

如何将正常图像设置为按钮的背景?

以及如何在xaml中设置此按钮的背景onmouseover事件的另一个图像?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-14 15:44:53

我对按钮使用样式,如下所示:

代码语言:javascript
复制
<Style x:Key="minusButtonStyle" TargetType="Button">
 <Setter Property="ToolTipService.ToolTip" Value="Уменьшить масштаб графика, Ctrl + вниз" />
 <Setter Property="Width" Value="35"/>
 <Setter Property="Template">
  <Setter.Value>
   <ControlTemplate TargetType="Button">
    <Grid>
     <vsm:VisualStateManager.VisualStateGroups>
      <vsm:VisualStateGroup x:Name="CommonStates">
       <vsm:VisualState x:Name="Normal"/>
       <vsm:VisualState x:Name="MouseOver">
        <Storyboard>
         <DoubleAnimation Duration="0" Storyboard.TargetName="hoverImage" Storyboard.TargetProperty="Opacity" To="1"/>
        </Storyboard>
       </vsm:VisualState>
       <vsm:VisualState x:Name="Pressed">
        <Storyboard>
         <DoubleAnimation Duration="0" Storyboard.TargetName="pressedImage" Storyboard.TargetProperty="Opacity" To="1"/>
        </Storyboard>
       </vsm:VisualState>
       <vsm:VisualState x:Name="Disabled">
        <Storyboard>
         <DoubleAnimation Duration="0" Storyboard.TargetName="unactiveImage" Storyboard.TargetProperty="Opacity" To="1"/>
        </Storyboard>
       </vsm:VisualState>
      </vsm:VisualStateGroup>
      <vsm:VisualStateGroup x:Name="FocusStates">
       <vsm:VisualState x:Name="Focused"/>
       <vsm:VisualState x:Name="Unfocused" />
      </vsm:VisualStateGroup>
     </vsm:VisualStateManager.VisualStateGroups>
     <Border x:Name="normalImage" Opacity="1">
      <Border.Background>
       <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="Images/minus_unhover.png" Opacity="1.0"></ImageBrush>
      </Border.Background>
     </Border>
     <Border x:Name="hoverImage" Opacity="0">
      <Border.Background>
       <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="Images/minus_hover.png" Opacity="1.0"></ImageBrush>
      </Border.Background>
     </Border>
     <Border x:Name="pressedImage" Opacity="0">
      <Border.Background>
       <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="Images/minus_pressed.png" Opacity="1.0"></ImageBrush>
      </Border.Background>
     </Border>
     <Border x:Name="unactiveImage" Opacity="0">
      <Border.Background>
       <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="Images/minus_unactive.png" Opacity="1.0"></ImageBrush>
      </Border.Background>
     </Border>
     <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}"/>
    </Grid>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>  

并将此样式应用于按钮:

代码语言:javascript
复制
<Button x:Name="minus_button" Style="{StaticResource minusButtonStyle}" ClickMode="Release"></Button>  

我认为这是一个最好的解决方案,因为你可以在另一个状态上显示你想要的东西,并控制可见的效果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12002813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档