Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace Dubsmash.Controls
- {
- public class PlatformTextBox : TextBox
- {
- private ContentControl PlaceholderElement;
- public PlatformTextBox()
- {
- DefaultStyleKey = typeof(PlatformTextBox);
- TextChanged += OnTextChanged;
- }
- public override void OnApplyTemplate()
- {
- PlaceholderElement = (ContentControl)GetTemplateChild("PlaceholderElement");
- DeterminePlaceholderElementVisibility();
- base.OnApplyTemplate();
- }
- private void DeterminePlaceholderElementVisibility()
- {
- if (string.IsNullOrEmpty(this.Text))
- {
- PlaceholderElement.Visibility = Visibility.Visible;
- }
- else
- {
- PlaceholderElement.Visibility = Visibility.Collapsed;
- }
- }
- private void OnTextChanged(object sender, TextChangedEventArgs e)
- {
- var binding = this.GetBindingExpression(TextBox.TextProperty);
- if (binding != null)
- binding.UpdateSource();
- DeterminePlaceholderElementVisibility();
- }
- #region PlaceholderText
- public string PlaceholderText
- {
- get { return (string)GetValue(PlaceholderTextProperty); }
- set { SetValue(PlaceholderTextProperty, value); }
- }
- public static readonly DependencyProperty PlaceholderTextProperty =
- DependencyProperty.Register("PlaceholderText", typeof(string), typeof(PlatformTextBox), new PropertyMetadata(string.Empty));
- #endregion
- #region IsPassword
- public bool IsPassword
- {
- get { return (bool)GetValue(IsPasswordProperty); }
- set { SetValue(IsPasswordProperty, value); }
- }
- public static readonly DependencyProperty IsPasswordProperty =
- DependencyProperty.Register("IsPassword", typeof(bool), typeof(PlatformTextBox), new PropertyMetadata(false, OnIsPasswordChanged));
- private static void OnIsPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if((bool)e.NewValue)
- {
- (d as PlatformTextBox).FontFamily = new FontFamily("/Assets/Fonts/password.ttf#Password");
- }
- else
- {
- (d as PlatformTextBox).FontFamily = (FontFamily)App.Current.Resources["PhoneFontFamilyNormal"];
- }
- }
- #endregion
- }
- }
- <ResourceDictionary
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Dubsmash.Controls">
- <Style TargetType="local:PlatformTextBox">
- <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
- <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
- <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
- <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
- <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
- <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
- <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
- <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
- <Setter Property="Padding" Value="2"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="local:PlatformTextBox">
- <Grid Background="Transparent">
- <VisualStateManager.VisualStateGroups>
- <VisualStateGroup x:Name="CommonStates">
- <VisualState x:Name="Normal"/>
- <VisualState x:Name="MouseOver"/>
- <VisualState x:Name="Disabled">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- <VisualState x:Name="ReadOnly">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="MainBorder">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <Visibility>Collapsed</Visibility>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ReadonlyBorder">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <Visibility>Visible</Visibility>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ReadonlyBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ReadonlyBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- </VisualStateGroup>
- <VisualStateGroup x:Name="FocusStates">
- <VisualState x:Name="Focused">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder">
- <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- <VisualState x:Name="Unfocused"/>
- </VisualStateGroup>
- </VisualStateManager.VisualStateGroups>
- <Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}"/>
- <Border x:Name="ReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed"/>
- <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
- <ContentControl x:Name="PlaceholderElement" Content="{TemplateBinding PlaceholderText}" Foreground="{StaticResource PhoneDisabledBrush}" FontFamily="{StaticResource PhoneFontFamilyNormal}" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
- </Border>
- <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
- <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ResourceDictionary>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement