Customize your own Silverlight control
2009-09-10 13:20
435 查看
1. Create a new class which inherit from ContentControl
2. You must create Themes folder in Silverlight application.
3. Create a txt file and rename it to Generic.xaml under the Themes folder.
4. In the Generic.xaml, paste the following content in,(NOTICE: Namespaces)
5. In other pages, you can use it in these ways,
a. Using it in a simple way
b. Using it with new content.(replace the original content with yours)
namespace SimpleButtonDemo { public class SimpleButton : ContentControl { public event RoutedEventHandler Click; public SimpleButton() { DefaultStyleKey = typeof(SimpleButton); this.MouseLeftButtonUp += new MouseButtonEventHandler(SimpleButton_MouseLeftButtonUp); this.MouseEnter += new MouseEventHandler(SimpleButton_MouseEnter); this.MouseLeave += new MouseEventHandler(SimpleButton_MouseLeave); } void SimpleButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Click != null) Click(this, new RoutedEventArgs()); } void SimpleButton_MouseEnter(object sender, MouseEventArgs e) { VisualStateManager.GoToState(this, "MouseOver", true); } void SimpleButton_MouseLeave(object sender, MouseEventArgs e) { VisualStateManager.GoToState(this, "Normal", true); } } }
2. You must create Themes folder in Silverlight application.
3. Create a txt file and rename it to Generic.xaml under the Themes folder.
4. In the Generic.xaml, paste the following content in,(NOTICE: Namespaces)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:custom="clr-namespace:SimpleButtonDemo;assembly=SimpleButtonDemo" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"> <Style TargetType="custom:SimpleButton"> <Setter Property="Width" Value="100" /> <Setter Property="Height" Value="100" /> <Setter Property="Background" Value="Lavender" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="custom:SimpleButton"> <!--<Grid x:Name="RootElement"> <Rectangle x:Name="BodyElement" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Fill="{TemplateBinding Background}" Stroke="Purple" RadiusX="16" RadiusY="16" /> <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>--> <Grid x:Name="RootElement"> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition To="Normal"/> <vsm:VisualTransition To="MouseOver"/> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name="Normal" /> <vsm:VisualState x:Name="MouseOver"> <Storyboard Duration="0:0:.8"> <ColorAnimation Storyboard.TargetName="BodyElement" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="Yellow" /> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Rectangle x:Name="BodyElement" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Fill="{TemplateBinding Background}" Stroke="Purple" RadiusX="16" RadiusY="16" /> <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
5. In other pages, you can use it in these ways,
a. Using it in a simple way
<custom:SimpleButton Canvas.Left="300" Canvas.Top="300" Content="New Button" Width="100" Height="100" Background="Bisque" Click="SimpleButton_Click_1" />
b. Using it with new content.(replace the original content with yours)
<custom:SimpleButton Width="250" Height="150" Click="SimpleButton_Click"> <custom:SimpleButton.Content> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Ellipse Width="75" Height="75" Margin="10"> <Ellipse.Fill> <RadialGradientBrush GradientOrigin="0.25,0.25"> <GradientStop Offset="0.25" Color="White" /> <GradientStop Offset="1.0" Color="Red" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <TextBlock Text="Click Me" VerticalAlignment="Center" /> </StackPanel> </custom:SimpleButton.Content> </custom:SimpleButton>
相关文章推荐
- Customize Your Own Linux
- customize your own rotation behavior
- Customize your own MFC message
- Methods to Control Your Own Feelings Two
- Methods to Control Your Own Feelings One
- Methods to Control Your Own Feelings Two
- How to Scale your Silverlight Control
- Milestone/A853/XT702/umts_sholes Compile your own Android ROM and Customize your own linux kernel
- Embed an HTML control in your own window using plain C
- Embed an HTML control in your own window using plain C
- customize your own memory allocator (1)
- customize your own memory allocator (2)
- Embed an HTML control in your own window using plain C
- Embed an HTML control in your own window using plain C
- How to register your own or the third ActiveX Control to the target PC using InstallShield 11.5 Pre Edition?
- Customize web.config sections and register your own configuration then get values in asp.net
- Implementing your own base class for user controls in Silverlight 2
- How Tomcat Works, A Guide to Developing Your Own Java Servlet Container
- Create your own HttpContext class>> from 123aspx.net
- make your own phpstorm epa