In the previous article "use ListBox in Windows Phone 7", I mentioned how to use The ListBox control in Windows Phone 7 and bind data to the control, take a book sales list as an example. Next we will try to update the UI of this example to make the entire interface look a little more beautiful (looking back at the end, it seems not so nice ). Embed a button in ListBox and add the gradient effect to the button.
First, open the project booklist of the previous article, and open the app in the solution window on the right. XAML file (the role of this file is described in the first WP7 project: Hello WP7). We will create a control module in this file. Find the Code:
<Application.Resources> </Application.Resources>
Add our control module in the Code. The Code is as follows:
<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>
After adding the control template, return to our mainpage. XAML.
<DataTemplate>
Add a button control and set the control style as the template style we just compiled. The code after adding the button is as follows:
<DataTemplate> <Button Width="460" Height="120" Style="{StaticResource MyButton}"> <Button.Content> <StackPanel Orientation="Horizontal">
The concluding remarks are as follows:
</StackPanel> </Button.Content> </Button> </DataTemplate>
Press F5 to compile, and the final result is as follows: