zoukankan      html  css  js  c++  java
  • [No0000124]WPF 扩展控件Behavior的几种方式

    一、使用Attached Dependency Property的方式

    (1)定义Attached Dependency Property

     public static class DigitsOnlyBehavior
        {
            public static bool GetIsDigitOnly(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsDigitOnlyProperty);
            }
    
            public static void SetIsDigitOnly(DependencyObject obj, bool value)
            {
                obj.SetValue(IsDigitOnlyProperty, value);
            }
    
            public static readonly DependencyProperty IsDigitOnlyProperty =
              DependencyProperty.RegisterAttached("IsDigitOnly",
              typeof(bool), typeof(DigitsOnlyBehavior),
              new PropertyMetadata(false, OnIsDigitOnlyChanged));
    
            private static void OnIsDigitOnlyChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                // ignoring error checking
                var textBox = (TextBox)sender;
                var isDigitOnly = (bool)(e.NewValue);
    
                if (isDigitOnly)
                    textBox.PreviewTextInput += BlockNonDigitCharacters;
                else
                    textBox.PreviewTextInput -= BlockNonDigitCharacters;
            }
    
            private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
            {
                e.Handled = e.Text.Any(ch => !Char.IsDigit(ch));
            }
        }

    使用上面定义好的Attached Dependency Property

    <TextBox Grid.Row="0" behaviors:DigitsOnlyBehavior.IsDigitOnly="True" Margin="6"/>

    二、使用Behavior<T>

    (1) 定义Behavior

    public class DragBehavior : Behavior<UIElement>
    {
        private Point elementStartPosition;
        private Point mouseStartPosition;
        private TranslateTransform transform = new TranslateTransform();
    
        protected override void OnAttached()
        {
            Window parent = Application.Current.MainWindow;
            AssociatedObject.RenderTransform = transform;
    
            AssociatedObject.MouseLeftButtonDown += (sender, e) => 
            {
                elementStartPosition = AssociatedObject.TranslatePoint( new Point(), parent );
                mouseStartPosition = e.GetPosition(parent);
                AssociatedObject.CaptureMouse();
            };
    
            AssociatedObject.MouseLeftButtonUp += (sender, e) =>
            {
                AssociatedObject.ReleaseMouseCapture();
            };
    
            AssociatedObject.MouseMove += (sender, e) =>
            {
                Vector diff = e.GetPosition( parent ) - mouseStartPosition;
                if (AssociatedObject.IsMouseCaptured)
                {
                    transform.X = diff.X;
                    transform.Y = diff.Y;
                }
            };
        }
    }

    (2)使用Behavior

    <Border Background="LightBlue" >
        <e:Interaction.Behaviors>
            <b:DragBehavior/>
        </e:Interaction.Behaviors>
        <TextBlock Text="Drag me around!" />
    </Border>
  • 相关阅读:
    一起谈.NET技术,抛砖引玉:我看微软.NET各子技术领域之应用前景 狼人:
    一起谈.NET技术,Windows Mobile 6.5的开始菜单 狼人:
    一起谈.NET技术,ASP.NET MVC 验证方式(1) 狼人:
    一起谈.NET技术,强烈推荐体验VisualStudio2010 RC 狼人:
    【华安php入门系列】第1天php的执行方式、执行过程
    败者树Java实现
    Android INSTALL_FAILED_ACWF_INCOMPATIBLE
    《构建高质量的C#代码》,新书上市,欢迎交流!
    oracle中的左右连接
    VS2012 UPDATE 2 发布了离线包
  • 原文地址:https://www.cnblogs.com/Chary/p/No0000124.html
Copyright © 2011-2022 走看看