• 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>

  • 相关阅读:
    扫面线模板
    (动态规划、栈)leetcode 84. Largest Rectangle in Histogram, 85. Maximal Rectangle
    tmux 常见命令汇总
    leetcode 221
    leetcode 319 29
    (贪心)leetcode 392. Is Subsequence, 771. Jewels and Stones, 463. Island Perimeter
    leetcode 982 668
    Python import 同文件夹下的py文件的函数,pycharm报错
    Windows里Anaconda-Navigator无法打开的解决方案
    Windows下 gpu版 Tensorflow 安装
  • 原文地址:https://www.cnblogs.com/chuncn/p/1752226.html
Copyright © 2020-2023  润新知