zoukankan      html  css  js  c++  java
  • Behaviors and Subclassing in Silverlight4

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public static class TextBoxProperties
        {
            public static readonly DependencyProperty SelectAllProperty =
                DependencyProperty.RegisterAttached("SelectAll", typeof(bool),
                typeof(TextBoxProperties), new PropertyMetadata(false, OnSelectAllChanged));

            public static void SetSelectAll(DependencyObject o, bool value)
            {
                o.SetValue(SelectAllProperty, value);
            }

            public static bool GetSelectAll(DependencyObject o)
            {
                return (bool)o.GetValue(SelectAllProperty);
            }

            private static void OnSelectAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if ((bool)e.NewValue == true)
                {
                    ((TextBox)d).GotFocus += new RoutedEventHandler(TextBoxProperties_GotFocus);
                }
            }

            private static void TextBoxProperties_GotFocus(object sender, RoutedEventArgs e)
            {
                ((TextBox)sender).SelectAll();
            }
        }
    }

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Interactivity;

    namespace SilverlightApplication1
    {
        public class SelectAllBehavior : Behavior<TextBox>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.GotFocus += new RoutedEventHandler(AssociatedObject_GotFocus);
            }

            void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
            {
                ((TextBox)sender).SelectAll();
            }
        }
    }

    <UserControl x:Class="SilverlightApplication1.MainPage3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
                
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:behaviors="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1"
    >
       
        <Grid x:Name="LayoutRoot" Background="White">
            <TextBox Name="textBox1" Width="120" Height="23" Text="computer" HorizontalAlignment="Left">
                <i:Interaction.Behaviors>
                    <behaviors:SelectAllBehavior/>
                </i:Interaction.Behaviors>

            </TextBox>

            <TextBox  Name="textBox2" Width="120" Height="23"  Text="中华人民共和国,china" HorizontalAlignment="Center" behaviors:TextBoxProperties.SelectAll="True"/>
        </Grid>
    </UserControl>

  • 相关阅读:
    HDU 4389 X mod f(x) [数位DP]
    HDU 4370 0 or 1 [01规划最短路]
    HDU 4371 Alice and Bob [简单博弈]
    HDU 4386 Quadrilateral [最大四边形面积]
    HDU 4387 Stone Game [博弈]
    HDU 4385 Moving Bricks [状态压缩DP]
    HDU 4372 Count the Buildings [组合数学]
    几个项目管理网
    计算机信息系统集成资质管理办法
    201005期蘑菇班信息系统项目管理师招生简章
  • 原文地址:https://www.cnblogs.com/chuncn/p/1752226.html
Copyright © 2011-2022 走看看