微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于在silverlight 中自定义按钮的问题

自定义按钮中,我们主要使用的是style 中设置ControlTemplate中相关内容来完成的。实例代码如下:


    <UserControl.Resources>
        <Style x:Key="ButtonStyle1" targettype="Button">
            <Setter Property="Width" Value="70"/>
            <Setter Property="Height" Value="70"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate targettype="Button">
                        <Grid>
                            <visualstatemanager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="normal"/>
                                    <VisualState x:Name="MouSEOver">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="0.8" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="rectangle">
                                                <discreteObjectKeyFrame KeyTime="0">
                                                    <discreteObjectKeyFrame.Value>
                                                        <Thickness>-2</Thickness>
                                                    </discreteObjectKeyFrame.Value>
                                                </discreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="disabled"/>
                                </VisualStateGroup>
                            </visualstatemanager.VisualStateGroups>
                            <Rectangle x:Name="rectangle" Fill="{TemplateBinding Background}" RadiusY="20" RadiusX="20" stroke="Black" ToolTipService.ToolTip="{TemplateBinding Tag}" strokeThickness="0"/>  <!--这里主要是用来绑定Button中的Background属性,可是绑定一张图片-->
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>


上面定义了button的样式,下面主要在button中引用样式,示例如下:


        <telerik:RadButton Margin="0,10,164,20" Name="radButton2" Style="{StaticResource ButtonStyle1}" ToolTipService.ToolTip="*********">
            <telerik:RadButton.Background>
                <ImageBrush ImageSource="图片路径" />             </telerik:RadButton.Background>         </telerik:RadButton>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐