每天进步一点!

导航

 
在ListBox中嵌入Button,并对Button添加渐变效果。
首先,打开上一篇文章的工程BookList,在右边的解决方案窗口中,打开App.xaml文件,我们将要在该文件建立一个控件模块。打开后找到这段代码:
    <Application.Resources>    </Application.Resources>在该代码里面添加我们的控件模块,在该代码里面添加我们的控件模块,代码如下:

    <Application.Resources>
        <Style x:Key="MyButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootElement">
                            <Rectangle Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" RadiusX="15" RadiusY="15">
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                        <GradientStop Color="Yellow" Offset="0.0" />
                                        <GradientStop Color="Red" Offset="0.25" />
                                        <GradientStop Color="Blue" Offset="0.75" />
                                        <GradientStop Color="LimeGreen" Offset="1.0" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                                <Rectangle.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#EC04FA" Offset="0" />
                                        <GradientStop Color="#FFFFFF" Offset="1" />
                                        </LinearGradientBrush>
                                </Rectangle.Stroke>
                                </Rectangle>
                            <ContentPresenter x:Name="contentPresenter" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>

添加完控件模板后,回到我们的MainPage.xaml中,在原来的

<DataTemplate>

中添加一个Button控件,并把控件风格设置为我们刚才编写的模板风格,添加后代码如下:

                    <DataTemplate>
                        <Button Width="460" Height="120" Style="{StaticResource MyButton}">
                            <Button.Content>
                                <StackPanel Orientation="Horizontal">

结束语句如下:

                                </StackPanel>
                            </Button.Content>
                        </Button>
                    </DataTemplate>


按下F5编译,最终效果图如下:


result2_thumb.jpg(17.27 KB, 下载次数: 0)

result2_thumb.jpg

posted on 2012-03-06 21:00  永远阳光  阅读(863)  评论(1)    收藏  举报