Windows Phone 7 自定义控件库

Windows Phone 7自定义一个控件库跟Silverlight的是基本一样的,第一步创建一个类库,然后添加一个Themes文件夹,在文件夹里面添加上generic.xaml文件作为默认的控件样式文件,记住一定要写这个名字否则就找不到样式了,大小写都可以。新建一个控件类MyContro1.cs,MyContro2.cs在这里面就可以写控件的处理逻辑了。

下面看一下一个水印控件的处理:

generic.xaml文件

 

 
 
  1. <ResourceDictionary 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:local="clr-namespace:Phone.Controls;assembly=Phone.Controls" 
  5.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  6.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     
  7.     mc:Ignorable="d" 
  8.     > 
  9.     <Style  TargetType="local:WatermarkedTextBox"> 
  10.         <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/> 
  11.         <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
  12.         <Setter Property="Background" Value="White"/> 
  13.         <Setter Property="Foreground" Value="#FF3880B2"/> 
  14.         <Setter Property="BorderBrush" Value="#FF3880B2"/> 
  15.         <Setter Property="SelectionBackground" Value="#FF3880B2"/> 
  16.         <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/> 
  17.         <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
  18.         <Setter Property="Padding" Value="2"/> 
  19.         <Setter Property="Template"> 
  20.             <Setter.Value> 
  21.                 <ControlTemplate TargetType="local:WatermarkedTextBox"> 
  22.                     <Grid Background="Transparent"> 
  23.                         <VisualStateManager.VisualStateGroups> 
  24.                             <VisualStateGroup x:Name="CommonStates"> 
  25.                                 <VisualState x:Name="Normal"/> 
  26.                                 <VisualState x:Name="MouseOver"/> 
  27.                                 <VisualState x:Name="Disabled"> 
  28.                                     <Storyboard> 
  29.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder"> 
  30.                                             <DiscreteObjectKeyFrame KeyTime="0"> 
  31.                                                 <DiscreteObjectKeyFrame.Value> 
  32.                                                     <Visibility>Collapsed</Visibility> 
  33.                                                 </DiscreteObjectKeyFrame.Value> 
  34.                                             </DiscreteObjectKeyFrame> 
  35.                                         </ObjectAnimationUsingKeyFrames> 
  36.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder"> 
  37.                                             <DiscreteObjectKeyFrame KeyTime="0"> 
  38.                                                 <DiscreteObjectKeyFrame.Value> 
  39.                                                     <Visibility>Visible</Visibility> 
  40.                                                 </DiscreteObjectKeyFrame.Value> 
  41.                                             </DiscreteObjectKeyFrame> 
  42.                                         </ObjectAnimationUsingKeyFrames> 
  43.                                     </Storyboard> 
  44.                                 </VisualState> 
  45.                                 <VisualState x:Name="ReadOnly"> 
  46.                                     <Storyboard> 
  47.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder"> 
  48.                                             <DiscreteObjectKeyFrame KeyTime="0"> 
  49.                                                 <DiscreteObjectKeyFrame.Value> 
  50.                                                     <Visibility>Collapsed</Visibility> 
  51.                                                 </DiscreteObjectKeyFrame.Value> 
  52.                                             </DiscreteObjectKeyFrame> 
  53.                                         </ObjectAnimationUsingKeyFrames> 
  54.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder"> 
  55.                                             <DiscreteObjectKeyFrame KeyTime="0"> 
  56.                                                 <DiscreteObjectKeyFrame.Value> 
  57.                                                     <Visibility>Visible</Visibility> 
  58.                                                 </DiscreteObjectKeyFrame.Value> 
  59.                                             </DiscreteObjectKeyFrame> 
  60.                                         </ObjectAnimationUsingKeyFrames> 
  61.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="DisabledOrReadonlyBorder"> 
  62.                                             <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> 
  63.                                         </ObjectAnimationUsingKeyFrames> 
  64.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="DisabledOrReadonlyBorder"> 
  65.                                             <DiscreteObjectKeyFrame KeyTime="0" Value="#FF3880B2"/> 
  66.                                         </ObjectAnimationUsingKeyFrames> 
  67.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DisabledOrReadonlyContent"> 
  68.                                             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/> 
  69.                                         </ObjectAnimationUsingKeyFrames> 
  70.                                     </Storyboard> 
  71.                                 </VisualState> 
  72.                             </VisualStateGroup> 
  73.                             <VisualStateGroup x:Name="FocusStates"> 
  74.                                 <VisualState x:Name="Focused"> 
  75.                                     <Storyboard> 
  76.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder"> 
  77.                                             <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> 
  78.                                         </ObjectAnimationUsingKeyFrames> 
  79.                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder"> 
  80.                                             <DiscreteObjectKeyFrame KeyTime="0" Value="#FF3880B2"/> 
  81.                                         </ObjectAnimationUsingKeyFrames> 
  82.                                     </Storyboard> 
  83.                                 </VisualState> 
  84.                                 <VisualState x:Name="Unfocused"/> 
  85.  
  86.                             </VisualStateGroup> 
  87.                         </VisualStateManager.VisualStateGroups> 
  88.                         <Border x:Name="EnabledBorder" BorderBrush="#FF3880B2" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
  89.                             <Grid> 
  90.                                 <ContentControl x:Name="watermarkContent"  HorizontalContentAlignment="Left" Style="{TemplateBinding WatermarkStyle}" Content="{TemplateBinding Watermark}" Background="Transparent" Opacity="0.5"/> 
  91.                                 <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/> 
  92.                             </Grid> 
  93.                         </Border> 
  94.                         <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="White" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed"> 
  95.                             <TextBox x:Name="DisabledOrReadonlyContent" Background="White" Foreground="{StaticResource PhoneDisabledBrush}" 
  96.                                      FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" 
  97.                                      FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}"   
  98.                                      SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}"   
  99.                                      TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}" Template="{StaticResource PhoneDisabledTextBoxTemplate}" /> 
  100.                         </Border> 
  101.                     </Grid> 
  102.                 </ControlTemplate> 
  103.             </Setter.Value> 
  104.         </Setter> 
  105.     </Style> 
  106.  
  107. </ResourceDictionary> 

WatermarkTextBox..cs

 

 
 
  1. using System.Windows;  
  2. using System.Windows.Controls;  
  3.  
  4. namespace Phone.Controls  
  5. {  
  6.     public class WatermarkedTextBox : TextBox  
  7.     {  
  8.         ContentControl WatermarkContent;  
  9.         public static readonly DependencyProperty WatermarkProperty =  
  10.       DependencyProperty.Register("Watermark", typeof(object), typeof(WatermarkedTextBox), new PropertyMetadata(OnWatermarkPropertyChanged));  
  11.  
  12.         public static readonly DependencyProperty WatermarkStyleProperty =  
  13.       DependencyProperty.Register("WatermarkStyle", typeof(Style), typeof(WatermarkedTextBox), null);  
  14.  
  15.         public Style WatermarkStyle  
  16.         {  
  17.             get { return base.GetValue(WatermarkStyleProperty) as Style; }  
  18.             set { base.SetValue(WatermarkStyleProperty, value); }  
  19.         }  
  20.  
  21.         public object Watermark  
  22.         {  
  23.             get { return base.GetValue(WatermarkProperty) as object; }  
  24.             set { base.SetValue(WatermarkProperty, value); }  
  25.         }  
  26.  
  27.         public WatermarkedTextBox()  
  28.         {  
  29.             DefaultStyleKey = typeof(WatermarkedTextBox);  
  30.         }  
  31.  
  32.         public override void OnApplyTemplate()  
  33.         {  
  34.             base.OnApplyTemplate();  
  35.             thisthis.WatermarkContent = this.GetTemplateChild("watermarkContent") as ContentControl;  
  36.             if(WatermarkContent != null)  
  37.             {  
  38.               DetermineWatermarkContentVisibility();  
  39.             }  
  40.         }  
  41.  
  42.         protected override void OnGotFocus(RoutedEventArgs e)  
  43.         {  
  44.             if (WatermarkContent != null && string.IsNullOrEmpty(this.Text))  
  45.             {  
  46.                 this.WatermarkContent.Visibility = Visibility.Collapsed;  
  47.             }  
  48.             base.OnGotFocus(e);  
  49.         }  
  50.  
  51.         protected override void OnLostFocus(RoutedEventArgs e)  
  52.         {  
  53.             if (WatermarkContent != null && string.IsNullOrEmpty(this.Text))  
  54.             {  
  55.                 this.WatermarkContent.Visibility = Visibility.Visible;  
  56.             }  
  57.             base.OnLostFocus(e);  
  58.         }  
  59.  
  60.         private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)  
  61.         {  
  62.             WatermarkedTextBox watermarkTextBox = sender as WatermarkedTextBox;  
  63.             if(watermarkTextBox != null && watermarkTextBox.WatermarkContent !=null)  
  64.             {  
  65.               watermarkTextBox.DetermineWatermarkContentVisibility();  
  66.             }  
  67.         }  
  68.  
  69.         private void DetermineWatermarkContentVisibility()  
  70.         {  
  71.             if (string.IsNullOrEmpty(this.Text))  
  72.             {  
  73.                 this.WatermarkContent.Visibility = Visibility.Visible;  
  74.             }  
  75.             else  
  76.             {  
  77.                 this.WatermarkContent.Visibility = Visibility.Collapsed;  
  78.             }  
  79.         }  
  80.     }  

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078436



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值